Ruby on rails 应为图像(#70365286921100),获取数组(#70365535770260)错误
我正在尝试创建一个简单的rails 3.2应用程序 为了简单起见,该应用程序有两种型号:产品和图像 该产品应该有许多图像,因此以下是我的模型:Ruby on rails 应为图像(#70365286921100),获取数组(#70365535770260)错误,ruby-on-rails,ruby,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3.2,我正在尝试创建一个简单的rails 3.2应用程序 为了简单起见,该应用程序有两种型号:产品和图像 该产品应该有许多图像,因此以下是我的模型: class Product < ActiveRecord::Base has_many :images, :class_name => 'Image' end class Image < ActiveRecord::Base belongs_to :product has_attached_file :image, :st
class Product < ActiveRecord::Base
has_many :images, :class_name => 'Image'
end
class Image < ActiveRecord::Base
belongs_to :product
has_attached_file :image, :styles => { :normal => "300x300", :small => "70x70" }
end
类产品“图像”
结束
类映像{:normal=>“300x300”,:small=>“70x70”}
结束
我正在使用active_admin,这是我创建产品的表单:
<%= semantic_form_for [:admin, @product], :html => {:multipart => true} do |f| %>
<%= f.inputs :title, :description, :price %>
<%= f.semantic_fields_for :images do |f2| %>
<%= f2.file_field :image %>
<% end %>
<%= f.buttons :commit %>
<% end %>
{:multipart=>true}do | f |%>
当我提交表格时,我得到以下例外情况:
Image(#70365286921100) expected, got Array(#70365535770260)
{"utf8"=>"✓",
"authenticity_token"=>"waFPhUIJPD90r5SRVmvvYBEcpZHgFJbM325wZDknWf8=",
"product"=>{"title"=>"rfrfrf",
"description"=>"rfrfr",
"price"=>"200.99",
"images"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007ffe63d19e58 @original_filename="IMG_0097.JPG",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"product[images][image]\"; filename=\"IMG_0097.JPG\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:/var/folders/_j/s1n6_4551cxc765p1zm8w54r0000gq/T/RackMultipart20120503-2609-bwvbis>>}},
"commit"=>"Create Product"}
Image(#70365286921100)应为,已获取数组(#70365535770260)
{“utf8”=>“✓",
“真实性令牌”=>“WaffuijPD90R5SRVMVVYBECPZHGFJBM325WZDKNWF8=”,
“产品”=>{“标题”=>“RFRF”,
“说明”=>“RFR”,
“价格”=>“200.99”,
“图像”=>{“图像”=>#},
“提交”=>“创建产品”}
为什么会这样?有人能帮我吗
提前感谢!我相信您需要
接受产品模型中图像的嵌套属性。产品模型应如下所示:
class Product < ActiveRecord::Base
has_many :images, :class_name => 'Image'
accepts_nested_attributes_for :images
end
接受
的_嵌套的_属性_所做的是更改参数的结构,以适应由has _many:images
关联指定的一对多关系
假设表单中有多个图像,则params散列将包含:
"images_attributes"=>{"0"=>{"image"=> ... }, "1"=>{"image" => ... }, ...}
另外,如果@product
是新产品,请确保在到达视图之前调用@product.images.build
"images_attributes"=>{"0"=>{"image"=> ... }, "1"=>{"image" => ... }, ...}