我有一個模型「文章」has_many「資產」這是一個多態模型,我附加圖像使用回形針。 當我編輯文章,我想能夠刪除舊圖像,並添加一個新的一個在同一筆畫。我正在使用fields_for,因爲the Rails API says我可以將它用於資產的特定實例,所以fields_for似乎具有多功能性。因此,這裏是我的表單的相關部分:如何刪除一個回形針附件,並創建另一個一舉
表:
=f.fields_for :assets do |ff|
=ff.label "image"
=ff.file_field :image
-unless @article.assets.first.image_file_name.nil?
[email protected] do |asset|
=f.fields_for :assets, asset do |fff|
=image_tag(asset.image.url(:normal))
=fff.label "delete image"
=fff.check_box :_destroy
第一fields_for
是添加圖片的文章,第二部分是刪除已經存在的資產。這種形式可以添加資產,刪除資產,但它不能同時進行。 這是問題。 我懷疑check_box
沒有足夠的指示或什麼。
資產型號:
class Asset < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
has_attached_file :image, :styles => { :normal => "100%",:small => "100 x100>",:medium => "200x200>", :thumb => "50x50>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:attachment/:id/:style/:filename"
條控制器/編輯:
def edit
@article = Article.find(params[:id])
@assets = @article.assets
if @assets.empty?
@article.assets.build
end
end
我期待您的答覆。