我有一个虚拟字段“delete_picture来删除回形针附加文件字段:
class Paragraph < ApplicationRecord
has_attached_file :picture, path: "....", url: " ...
attr_accessor :delete_picture
before_save do
picture.clear if delete_picture=="1"
end
end
在表单中,我在delete_picture
字段上有一个复选框,如果用户点击它并且提交,则仅当另一个字段也被更改时,图片才会被清除 ,如果没有,则before_save回调是不执行。
那么,如果仅修改attr_accesor
字段,我该如何进行回调运行?
模型嵌套在另一个模型中:
父模型:
class Menu < ApplicationRecord
has_many :paragraphs, dependent: :destroy, inverse_of: :menu
validates :paragraphs, presence: true
accepts_nested_attributes_for :paragraphs, :allow_destroy => true, :reject_if => proc { |obj| obj["content"].blank? and obj["picture"].nil? }
end
以及表单,菜单和字段_for段落
<%= form_for(@menu) do |f| %>
...
<%= f.fields_for :paragraphs do |paragrafs| %>
....
<%=paragrafs.check_box :delete_picture, class:"delete"%>
由于