gpt4 book ai didi

paperclip - Rails with Paperclip 忽略空附件

转载 作者:行者123 更新时间:2023-12-04 20:10:07 24 4
gpt4 key购买 nike

我正在使用带有导轨的回形针并且它工作正常,但是我的问题是更新时如果我不重新选择图像,那么它会将记录保存为 nil。这是我的代码:

<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>

和 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

型号:

书籍型号:
has_one :book_content, :dependent => :destroy  
accepts_nested_attributes_for :book_content, :allow_destroy => true

Book_content 模型:
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

这是一个嵌套表单,但因为它有多个附件所以我不能使用 reject_if
如果没有选择文件,如何告诉回形针保留原始文件?

谢谢

最佳答案

经过一番研究,我发现我的问题没有通过 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 ,现在它工作正常。

关于paperclip - Rails with Paperclip 忽略空附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18764426/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com