dml*_*ogs 5 ruby-on-rails paperclip nested-forms nested-attributes ruby-on-rails-3
我正在使用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 :images do |s| %>
<%= render 'image_fields', :f => s %>
<% end%>
</div>
<div class="field">
<%= link_to_add_fields "Add Image", f, :images %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
_image_fields:
<div class="fields">
<% if !f.object.new_record? %>
<%= image_tag f.object.image.url %>
<% else %>
<%= f.file_field :image%>
<% end %>
<%= link_to_remove_fields "remove", f %>
</div>
Run Code Online (Sandbox Code Playgroud)
Javascript函数:
function remove_fields(link){
$(link).previous("input[type=hidden]").value = "1";
$(link).up(".fields").hide();
}
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).up().insert({
before: content.replace(regexp, new_id)
});
}
Run Code Online (Sandbox Code Playgroud)
助手功能:
def link_to_remove_fields(name, f)
f.hidden_field(:_destroy) +
link_to_function(name, "remove_fields(this)")
end
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(association.to_s.singularize + "_fields", :f => builder)
end
link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
end
Run Code Online (Sandbox Code Playgroud)
正如我上面提到的,我能够添加图像.单击"添加图像"链接会向表单添加文件字段,我可以成功添加多个图像.单击图像的"删除"链接会从视图中删除链接和图像 - 但正如我所说,它不会在Post事件中删除.
先谢谢你; 我感谢任何花时间帮我找到解决方案的人.
编辑:
- 解 -
在Post Controller中,在更新方法的find中添加了".includes(:images)":
def update
@post = Post.includes(:images).find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
format.html { redirect_to(@post, :notice => 'Post was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end
Run Code Online (Sandbox Code Playgroud)
小智 9
我有一个非常相似的问题.我能够通过删除模型中的:reject_if子句来"解决"它.
accepts_nested_attributes_for :images, :allow_destroy => true
Run Code Online (Sandbox Code Playgroud)
一旦我删除它,我就可以删除我的字段没有问题.明显的缺点是用户现在可以添加空白/无效图像,模型不会拒绝它.
编辑:
看起来这实际上是Rails 3.0.4关于accepts_nested_attributes_for如何处理:allow_destroy和:reject_if的错误.请参阅此处的答案以获取另一种解决方法:accepted_nested_attributes_for和reject_if的Paperclip问题
| 归档时间: |
|
| 查看次数: |
5972 次 |
| 最近记录: |