开发者

CarrierWave Doesn't Delete Assets When Object#destroy Called?

I'm using CarrierWave in a Rails3 app to upload assets. All good on the upload side, but when the destroy method is invoked, the corresponding asset is not removed.

It looks like CarrierWave creates dynamic methods for this that one can use in the destroy method so if the model is avatar then methods like remove_avatar spring into existence.

However, I can't get any of that to work. Any hints much a开发者_如何学Goppreciated.


I'm sorry to answer my own question, but in my case, it was a simple case of overlooking the obvious: The CarrierWave model needed a string field to contain the path name. So my model is:

class SiteAsset < ActiveRecord::Base
  mount_uploader :asset, AssetUploader

  def store_dir
    'public/assets'
  end

  def extension_white_list
    %w(jpg jpeg gif png pdf doc docx xls xlsx)
  end
end

and the correct migration was:

class CreateSiteAssets < ActiveRecord::Migration
  def self.up
    create_table :site_assets do |t|
      t.string :title
      t.string :asset
      t.timestamps
    end
  end

  def self.down
    drop_table :site_assets
  end
end

The missing part of the incantation was that the "asset" string was missing in my migration. I guess I was just not seeing the requirement in the documents although good sense would have made it immediately obvious.


I ran into the same problem the other day. It worked with a simple form, but didn't seem to enjoy the *remove_model_name* on nested forms.

In the end I just sucked it up and used a hidden_field with :_destroy

For reference: https://github.com/galaxylord/carrierwave_nested

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜