Saturday, March 12, 2011

[Rails] Re: A file-upload suddenly seems to be nil

On Mar 12, 1:58 pm, Luke <kickingje...@gmail.com> wrote:
> That was the problem, thanks a lot! I changed it to
>
> @upload = Upload.new
> @upload.file = params[:file]
> @upload.save!
> @article.uploads << @upload
>
> works like a charm. @upload.file = params[:file] works because I'm using
> carrierwave to manage uploads.
>
> However, I'm wondering how you have to write your constructor to be able to
> pass the file when instantiating the model. I mean like this:
>
> @upload = Upload.create(params[:file])
>
You have to pass a hash of attribute names to values to create - you
can't just pass a value.
Files are just like any other attribute - you can't for example do
Person.create(params[:name]), you have to do Person.create(:name =>
params[:name]). In the same way you would do foo.uploads.create :file
=> params[:file]

Fred

--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate