On Fri, Mar 11, 2011 at 4:33 PM, Luke <[email protected]> wrote:

> Hi there,
>
> I posted this issue
> <https://groups.google.com/forum/?hl=de#%21topic/carrierwave/ergk9LaO68k>at
> the carrierwave-group, but I'm beginning to think this rather is a
> rails-issue than a problem with carrierwave. The problem's this:
>
> I have 2 models, 'article' and 'upload'. article has_many :uploads. In my
> article_controller i have an action named upload:
>
> def upload
>   @article = Article.find(params[:id])
>   @article.uploads.create(params[:file])
>   render :nothing => true
> end
>
> this should save a file-upload to article.uploads. I'm using carrierwave to
> save uploads in the upload-model, but that's probably not the problem.
> Before I created the has_many-association I saved the upload directly in the
> article-model using params[:file] in that exact same action and that
> worked great.
> Now that I created the association there seems to be a problem with
> params[:file]. I keep getting a NoMethodError:
>
> You have a nil object when you didn't expect it!
> You might have expected an instance of Array.
> The error occurred while evaluating nil.delete
>
> app/controllers/articles_controller.rb:95:in `upload'
>
> where line 95 is @article.uploads.create(params[:file])
>
> if I remove the parameter no errors get thrown. But the console tells me
> params[:file] DOES exists and has proper values. Also params[:file] worked
> great before I used the has_many association.
>
>
params[:file] is not the issue. Its the create method you are calling for
@article.uploads that is throwing the error. In your code, you are saying
that the create method is part of the upload model, because of your has_many
association, so it is looking there. Also, since you are doing a has_many
association between article and upload you will receive back an array. If
you want to access the method for an object in that array you will need to
loop over it or call the object in the array directly.

examples:

Loop over the array
@article.uploads.each {|u| u.create(params[:file])}

Call object in array by position
@article.uploads[0].create(params[:file])

Call first object in the arra
@article.uploads.first.create(params[:file])

B.

-- 
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.

Reply via email to