ruby-on-rails - 回形针在更新时删除现有附件

标签 ruby-on-rails file-upload paperclip

AR模型

class Post < ActiveRecord::Base
  has_one :sponsor
  accepts_nested_attributes_for :sponsor
end

class Sponsor < ActiveRecord::Base
  has_attached_file :logo
  belongs_to :post
end

Controller

def update
  if @post.update(post_params)
    flash[:success] = 'Post update sucessfully'
  else
    flash[:error] = @post.errors.full_messages.join(', ')
    render :edit
  end
end

def post_params
  params.require(:post).permit(:title, :content, sponsor_attributes: [:descriptive_text, :logo, :name, :website, :description])
end

这里,当更新帖子时,赞助商也会以嵌套形式更新。

但是在更新时,如果用户未选择任何图像,Paperclip 会删除现有附件。

如果用户在更新记录时没有选择其他附件,我们如何保留现有附件?

最佳答案

accepts_nested_attributes_for :sponsor, reject_if: :all_blank

reject_if .

不幸的是,你遇到的问题是,Paperclip 在接受数据方面实际上相当“愚蠢”。

当许多人将关联数据发送到 Paperclip 时,它基本上会获取所提供的数据并从中构建对象。就您而言,这意味着您正在发送空白对象,导致回形针将您现有的附件替换为空白对象。

accepts_nested_attributes_forreject_if 开关可以解决此问题 - 它允许您指定 Rails 将“拒绝”任何嵌套数据的任何情况,从而保留您所选择的文件。有...

reject_if

Allows you to specify a Proc or a Symbol pointing to a method that checks whether a record should be built for a certain attribute hash.

The hash is passed to the supplied Proc or the method and it should return either true or false. When no :reject_if is specified, a record will be built for all attribute hashes that do not have a _destroy value that evaluates to true.

Passing :all_blank instead of a Proc will create a proc that will reject a record where all the attributes are blank excluding any value for _destroy.


需要注意的是,如果您要更新图像以外的内容(我看到您有 :descriptive_text:logo:名称:网站:描述)。

在这种情况下,您需要将适当的数据传递给您的 Sponsor 模型(IE 没有 logo 参数):

def post_params
   sponsor_params = [:id, :descriptive_text, :logo, :name, :website, :description]
   sponsor_params -= [:logo] if (action_name == "update") && !params[:post][:sponsor_attributes][:logo]

   params.require(:post).permit(:title, :content, sponsor_attributes: sponsor_params)
end

我确信有更好的方法可以使用Paperclip validators来做到这一点,但目前以上应该足够了。


引用文献:

关于ruby-on-rails - 回形针在更新时删除现有附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33253190/

相关文章:

jsf - primefaces 文件上传无效的文件类型 doc, docx

ruby-on-rails-3 - Cloudinary 上传文件但按钮仍然显示 'No file chosen'

file-upload - Rich :fileUpload component in (RC 4. 3.4) 在 Apache Tomcat7 和 JSF 2.2 上工作吗? : "Request prolog cannot be read"

ruby-on-rails-4 - Rails 4 Paperclip with Devise,文件保存错误

ruby-on-rails-3 - Rails 3 - 如何替换回形针中的现有附件

ruby-on-rails - 暂存环境 : Password protect everything except for webhook

ruby-on-rails - Rails 应用程序中对/etc/localtime 的过多 stat 调用

ruby-on-rails - 如何将日期时间值格式化为更友好的格式?

ruby-on-rails - Rails Basecamp 风格子域最佳实践

ruby-on-rails - 将服务器上的回形针路径文件名更新为 s3