Loading Carrierwave with nested forms?

Not sure what's going on here, but I think my nested forms partially caused a problem for CarrierWave.

When I update the field with the downloaded file, nothing happens: there is no error, but nothing is saved.

I have a Household model with a has_many relationship with an Individuals model. The "Independent" model has a "pictures" downloader:

class Individual < ActiveRecord::Base belongs_to :household mount_uploader :picture, PictureUploader end 

In my views, I:

 = form_for @household, :html => {:multipart => true} do |f| 

and then call partial for individuals:

 = f.fields_for :individuals do |builder| = render 'individual_fields', :f => builder = f.submit 

Partial just has the following:

 = f.label :firstname, 'First' = f.text_field :firstname, :size => 10 = f.label :lastname, 'Last' = f.text_field :lastname, :size => 15 = f.file_field :picture 

The loaded image appears in the options:

 Started POST "/households/849" for 127.0.0.1 at 2011-02-15 15:45:16 -0500 Processing by HouseholdsController#update as HTML Parameters: {"...6/1/2008; Active 6/6", "individuals_attributes"=>{"0"=>{"firstname"=>"Hannah", ... "picture"=>#<ActionDispatch::Http::UploadedFile:0xb9fbd24 @original_filename="3.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"household[individuals_attributes][1][picture]\"; filename=\"3.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20110215-6498-ba4bp>>, "_destroy"=>"false", "id"=>"4077"}}}, "commit"=>"Update Household", "id"=>"849"} 

And it is saved in the tmp directory in the boot path. It was never stored in the database and was not included in the file system.

Any ideas?

+8
ruby-on-rails ruby-on-rails-3
source share
1 answer

Some possible solutions:

  • It looks like you have, but just to make sure you have accepts_nested_attributes on the home model?
  • Also, have you tried this without partial localization of the problem?
  • Do you have an Rmagick or minimagick on a PictureUploader?

And you also want to point out a known issue with Carrierwave and nested forms, as described in the Wiki Wiki .

The workaround is to add a method below:

 class Image < ActiveRecord::Base mount_uploader :image, ImageUploader def image=(val) if !val.is_a?(String) && valid? image_will_change! super end end end class Person < ActiveRecord::Base has_many :images accepts_nested_attributes_for :images end 
+7
source share

All Articles