我有供应商型号:
has_many :galleries
has_many :photos, :through=>:galleries
accepts_nested_attributes_for :galleries
图库模型:
has_many :photos
accepts_nested_attributes_for :photos
照片模特:
belongs_to :gallery
belongs_to :vendor
在供应商控制器中:
def new
@vendor=Vendor.new(vendor_params)
@vendor.galleries.build.photos.build
end
def vendor_params
params.require(:vendor).permit(galleries_attributes:[photos_attributes:[:photo=>[]]])
end
在vendor / new.html.erb
中<%= f.fields_for :galleries do |g| %>
<%= g.fields_for :photos do |p| %>
<%= render 'photo_fields', :f=> p %>
<%= link_to_add_association 'Add Photos',f,:photos %>
<% end %>
<% end %>
我收到错误以下错误:
Started POST "/vendors" for 127.0.0.1 at 2015-01-09 23:31:17 +0530
Processing by VendorsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"1fUkNShGMhZoT1EyYRB0v08XtVpY72C5yBXFLIFz5o0=", "vendor"=>{"galleries_attributes"=>{"0"=>{"gallery_name"=>"brochure", "photos_attributes"=>{"0"=>{"photo"=>#<ActionDispatch::Http::UploadedFile:0x007f4630246328 @tempfile=#<Tempfile:/tmp/RackMultipart20150109-5672-1w996al>, @original_filename="Screenshot", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"vendor[galleries_attributes][0][photos_attributes][0][photo]\"; filename=\"Screenshot\"\r\nContent-Type: image/png\r\n">}}}}}, "commit"=>"Continue"}
Completed 500 Internal Server Error in 3ms
ActiveRecord::AssociationTypeMismatch (Photo(#69969748596420) expected, got Array(#17246380)):
app/controllers/vendors_controller.rb:96:in `create'