我有物品型号
class Item < ActiveRecord::Base
has_many :images, dependent: :destroy
accepts_nested_attributes_for :images, :allow_destroy => true
end
和图像模型
class Image < ActiveRecord::Base
belongs_to :item
has_attached_file :picture, :styles => {:medium => "600x600>", :thumb => "100x100>"}, :default_url => "/images/:style/missing.png"
end
在控制器中,强参数
def item_params
params.require(:item).permit(:name, :description, :price, images_attributes: [:picture, :_destroy])
end
正如Cocoon READM所说的那样。
所以基本的想法是一个Item有几个Images,而这些Image又有这个paperclip字段picture
。然后使用Coccon 呈现表单
#views/admin/items/new.html.erb
<div class="field">
<div class="ui teal label">Imagen</div>
<%= f.fields_for :images do |image| %>
<%= render partial: 'image_fields', :f => image %>
<% end %>
</div>
#views/admin/items/_image_fields.html.erb
<div class="nested-fields">
<%= f.file_field :image, :class => "add-image-field" %>
<%= link_to_remove_association "Eliminar", f, :class => "ui red mini button" %>
</div>
在视图中一切似乎都运行正常,但随后查看发送到控制器的参数,出了点问题:
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"AIogvRQuKNrAAOjM7YsnVQx7j2MwZ1VC3LiZy0cDDrU=",
"item"=>{
"name"=>"",
"description"=>"",
"price"=>"12345",
"images_attributes"=>{
"1394577880921"=>{
"image"=>#<ActionDispatch::Http::UploadedFile:0x00000003182718 @tempfile=#<Tempfile:/tmp/RackMultipart20140311-27326-19i0obp>, @original_filename="blue-diamond-rings.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"item[images_attributes][1394577880921][image]\"; filename=\"blue-diamond-rings.jpg\"\r\nContent-Type: image/jpeg\r\n">, "_destroy"=>"false"}}},
"commit"=>"Crear"}
images_attributes之后的奇怪签名是什么?它破坏了项目保存过程。
感谢任何指针!
答案 0 :(得分:3)
您调用file_field image
,它应该是picture
。
其余的都很好。
什么是#34;怪异的&#34;在image_attributes
内:
Image
s。image
(应该命名为picture
)的值是文件数据,当然看起来很奇怪