On 08 Jan 2009, at 18:35, Dave Smith wrote:
>> You are manually assigning related record ids instead of relying on
>> Rails' built-in safeguards.
>>
>> I'd advise u to start looking at railscasts.com, starting for very
>> early ones (complex forms might be a good one for you), getting a
>> Rails book (The Rails Way, Agile Web Development with Rails) and
>> getting to know the framework better.
>
> Many thanks for your extensive help on this one. I now have
>
> @project_file = ProjectFile.new(params[:project][:uploaded_data])
>
> in my projects controller.
>
>
> <%= f.file_field :uploaded_data %>
>
> but it still says
>
> ActiveRecord::UnknownAttributeError in ProjectsController#create
>
> unknown attribute: uploaded_data
Yes it will. The problem is this: your new project file object expects
the attribute :uploaded_data to be present in the hash you are passing
it.
@project_file = ProjectFile.new(params[:project])
This will pass in a hash that's too large (since it also holds your
project related attributes, but it will probably save the object for
you. That's the reason you need to get familiar with fat models,
skinny controllers. It will make mass assignments and related object
creation so much easier.
The only code that should be in your controller is that to assign the
instance variable with a newly created object. I would then let my
model handle the object creation, including the related record. In
fact, that is the exact thing the model is there for.
def create
@project = Project.create(params[:project])
end
Best regards
Peter De Berdt
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---