【发布时间】:2014-02-17 20:13:06
【问题描述】:
我希望能够使用 Active_admin 中的 formtastic 删除我的编辑表单中的图像。有很多关于此的帖子,但由于某种原因,我似乎无法让它适合我的设置。
我有一个 Post 模型和一个 NewsImages 模型,其中包含每个帖子的图像:
class Post < ActiveRecord::Base
has_many :news_images, dependent: :destroy
accepts_nested_attributes_for :news_images, allow_destroy: true
end
class NewsImage < ActiveRecord::Base
belongs_to :post
has_attached_file :photo
end
因此,从我所读到的内容中,可以将一个标志添加到我的 NewsImage 模型中,并有一个 before save 方法可以删除该图像。我设想它看起来像这样,但单击复选框时不会删除图像。
#/admin/post.rb
f.has_many :news_images do |p|
if p.object.new_record?
p.input :photo, as: :file
else
p.input :photo, as: :file, :hint => p.template.image_tag(p.object.photo.url(:thumb))
p.input :remove_image, as: :boolean, required: :false, label: 'Remove image'
end
end
在这个阶段我在控制台中注意到的一点是,当点击进出复选框时,它的值不会变为选中或未选中;应该这样吗?
NewsImage 模型现在看起来像
class NewsImage < ActiveRecord::Base
before_save :remove_photo
belongs_to :post
private
def remove_photo
self.photo.destroy if self.remove_image == '1'
end
end
这里有什么会导致这不起作用,或者有人有这种设置的解决方案吗?
【问题讨论】:
标签: ruby-on-rails ruby paperclip activeadmin formtastic