- 首页
- 其他开发
- Rails with Paperclip 忽略空附件
Rails with Paperclip 忽略空附件
[英] Rails with Paperclip ignore empty attachments
本文介绍了Rails with Paperclip 忽略空附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用带导轨的回形针并且它工作正常,但是我的问题是更新时如果我不重新选择图像,那么它会将记录保存为 nil.这是我的代码:
<div class="media"><%= image_tag program_avatar(b), :id =>'头像', :class =>'缩略图媒体对象向左拉', :height =>100, :width =>100%><div class="media-body padding-top-40"><%= b.file_field :avatar, :class =>'文件上传'%>
<div class="form-group"><div class="media"><%= image_tag program_banner(b), :class =>'缩略图媒体对象向左拉', :height =>100, :width =>300%><div class="media-body padding-top-40"><%= b.file_field :banner, :class =>'文件上传'%>
和控制器:
respond_to do |format|如果@program.update(program_params)format.html { redirect_to(program_path(@program), :notice => "程序更新") }format.js { 渲染:json =>@program.json }别的format.html { 渲染 :new, :notice =>错误请重试"}format.js { 渲染:json =>错误请重试"}结尾结尾
模型:
图书模型:
has_one :book_content, :dependent =>:破坏accepts_nested_attributes_for :book_content, :allow_destroy =>真的
图书内容模型:
has_attached_file :avatar, :styles =>{:中 =>"300x300>", :thumb =>100x100"}, :default_url =>"/images/:style/missing.png"has_attached_file :banner, :styles =>{ :header =>"600x150", :setting =>"300x100" }, :default_url =>"/images/:style/found.jpeg"归属地:书
这是一个嵌套表单,但因为它有多个附件所以我不能使用 reject_if
如果没有选择文件,如何告诉回形针保留原始文件?
谢谢
解决方案
经过一些研究,我发现我的问题不是在 params 中传递 id
,所以我有这个:
params.require(:book).permit(:book_name, book_content_attributes: [:media, :rating, :book_id])
改成这样:
params.require(:book).permit(:book_name, book_content_attributes: [:id, :media, :rating, :book_id])
注意添加的 :id
,现在它可以正常工作了.
I am using paperclip with rails and it works fine, however my problem is when updating if the i don't reselect the image, then it saves the record as nil. Here is my code:
<div class="form-group">
<div class="media">
<%= image_tag program_avatar(b), :id => 'avatar', :class => 'thumbnail media-object pull-left', :height => 100, :width => 100 %>
<div class="media-body padding-top-40">
<%= b.file_field :avatar, :class => 'file-upload' %>
</div>
</div>
</div>
<div class="form-group">
<div class="media">
<%= image_tag program_banner(b), :class => 'thumbnail media-object pull-left', :height => 100, :width => 300 %>
<div class="media-body padding-top-40">
<%= b.file_field :banner, :class => 'file-upload' %>
</div>
</div>
</div>
and the controller:
respond_to do |format|
if @program.update(program_params)
format.html { redirect_to(program_path(@program), :notice => "Program updated") }
format.js { render :json => @program.json }
else
format.html { render :new, :notice => "Error please try again" }
format.js { render :json => "Error please try again" }
end
end
The models:
Book model:
has_one :book_content, :dependent => :destroy
accepts_nested_attributes_for :book_content, :allow_destroy => true
Book_content model:
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
has_attached_file :banner, :styles => { :header => "600x150", :setting => "300x100" }, :default_url => "/images/:style/found.jpeg"
belongs_to :book
this is a nested form, but because it has multiple attachments so i cannot use reject_if
How can tell paperclip to keep the original files if no file is selected?
Thank you
解决方案
After some research I found that my problem is not passing the id
in the params, so I have this:
params.require(:book).permit(:book_name, book_content_attributes: [:media, :rating, :book_id])
Changed it to this:
params.require(:book).permit(:book_name, book_content_attributes: [:id, :media, :rating, :book_id])
notice the added :id
, and now it works fine.
这篇关于Rails with Paperclip 忽略空附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!