2

おやすみなさい。 Carrierwaveのネストされたフォームで資産を削除するためのソリューションをお使いの人はいますか?nested_formのassetをcarrierwave経由で削除するにはどうすればよいですか?

MODEL

has_many :article_images, :dependent => :destroy  
accepts_nested_attributes_for :article_images 

mount_uploader :image, ImageUploader  
belongs_to :article, :polymorphic => true 

schema.rb

create_table "article_images", :force => true do |t| 
t.string "image" 
t.string "article_id" 
end 

create_table "articles", :force => true do |t| 
t.string "title" 
end 

CONTROLLER

def edit 
@article = Article.find(params[:id]) 
@article.article_images.build 
end 

VIEW

_form.html.erb 
<%= f.fields_for :article_images do |article_image| %> 
<% if article_image.object.new_record? %> 
<%= article_image.file_field :image %> 
<% else %> 
<%= image_tag(article_image.object.image.url(:thumb)) %> 
<%= article_image.check_box :remove_image %> #DON'T WORK 
<% end %> 
<% end %> 
+0

[https://github.com/itsNikolay/carrierwave_multiply_files_upload](https://github.com/itsNikolay/carrierwave_multiply_files_upload)すべてのコードとレポがあります。 – itsnikolay

答えて

2

あなたのモデルであなたのaccepts_nested_attributes_forにこれを追加した場合どうなりますか:

accepts_nested_attributes_for :article_images, :allow_destroy => true 

とビューのコードでこれを変更します。これに

<%= article_image.check_box :remove_image %> #DON'T WORK 

<%= article_image.check_box :_destroy %> #MIGHT WORK? 
+0

これは機能しますが、空のフォルダの後にとどまります。 – itsnikolay

+0

あなたのための愛アダム:)オブジェクトをdistroyにnested_for _destroyを使用する;) –

4

を、私はそれが良いことだと思いますこれを行うと:

class ArticleImage < ActiveRecord::Base 
    # ... 
    attr_accessible :remove_image 
    after_save :clean_remove_image 

    def changed? 
    !!(super || remove_image) 
    end 

    def clean_remove_image 
    self.remove_image = nil 
    end 
end 

それは私のために働いた。

+0

私は実際にMongoidの古いバージョンで同様の問題に遭遇しました。ちょうど '変更? 'メソッドのオーバーライドが私のために働いた。イメージの削除を試みている間に、モデル内の別のフィールドも同時に更新されない限り、ネストされたオブジェクトはダーティとマークされていないように見えました。 –

関連する問題