我正在使用Paperclip 2.3.8运行Rails 3.0.3.我有两个模型,称它们为"Post"和"Image".Image是一个包含Paperclip图像文件的对象,用于动态地将图像添加到Posts.图片属于Post,Post有很多图片.
邮政模式:
class Post < ActiveRecord::Base
has_many :images
accepts_nested_attributes_for :images, :reject_if => lambda { |a| a[:image].blank? }, :allow_destroy => true
end
Run Code Online (Sandbox Code Playgroud)
图像模型:
class Image < ActiveRecord::Base
belongs_to :post
has_attached_file :image,
:styles => {
:thumb=> "100x100#",
:small => "300x300>" }
end
Run Code Online (Sandbox Code Playgroud)
我有一个表单,我想动态添加和删除图像.如果图像是正在上传的新图像,我想显示file_field; 否则,如果图像已经存在,我想显示图像和删除链接.我跟着Ryan Bates在嵌套表格上的Railscasts.动态添加图像是有效的,但是对_destory的调用没有触发.帖子包含"_destroy"=>"1",但在HTTP Post事件之后,id为7的图像仍然存在.以下是HTTP Post的摘录:
"images_attributes"=>{
"0"=>{"id"=>"7", "_destroy"=>"1"},
"1"=>{"id"=>"8", "_destroy"=>"false"},
"2"=>{"id"=>"9", "_destroy"=>"false"},
"3"=>{"id"=>"10", "_destroy"=>"false"}}
Run Code Online (Sandbox Code Playgroud)
这是表单的样子:
<%= form_for @post, :html => { :multipart => true } do |f| %>
<%= f.label :images, 'Images' %><br />
<div class="field">
<%= f.fields_for …Run Code Online (Sandbox Code Playgroud) ruby-on-rails paperclip nested-forms nested-attributes ruby-on-rails-3