开发者

paperclip custom :path and :url

I have some problems trying to custom the :path and :url options for has_attached_file with paperclip:

I have a polymorphic class named "Asset" that have :

class Asset < ActiveRecord::Base

  belongs_to :file_owner, :polymorphic => true

  has_attached_file :picture, :styles => { ...},
             :url => "/attachments/user_:user/dressing_:dressing/garment_:garment/category_:category/:basename_:style.:extension",  
             :path => ":rails_root/public/attachments/user_:user/dressing_:dressing/garment_:garment/category_:category/:basename_:style.:extension"  
end

The interpolations works well but I want to custom th开发者_运维百科e path and the url depending on the file_owner_type

for instance, if I want the user's picture path, I would like to just have

:path => ":rails_root/public/attachments/user_:user/:basename_:style.:extension

Thanks for your help

edit : I think I did not explain myself correctly. I have already the interpolations that are created and works well.

I have an asset model that is polymorphic, the owner can be a user (for is avatar), a garment or a dressing. And I want to have a different path depending on the file owner. At this time, when I want to add a garment asset it works well the picture is put in

"/attachments/user_x/dressing_y/garment_z/category_u/something_style.jpg"

but if I just want a user picture this path will put the avatar in

"/attachments/user_x/dressing_/garment_/category_/something_style.jpg"

whereas I want to put it in

"/attachments/user_x/something_style.jpg".

thanks


Something like this in the url:

:url => "/attachments/:path/:basename_:style.:extension",  

then in the interpolations:

Paperclip.interpolates :path do |attachment, style|
  if attachment.instance.file_owner_type == User.class.name
    # first set the _user variable (something like self.owner.id.to_s) 
    return "user_" + _user
  else
    # first set the _user, _dressing, _garmet, _category variables from your models
    return "user_#{_user}/dressing_#{_dressing}/garment_#{_garmet}/category_#{_category}/"
  end
end

Notice that you need to set the _user, _dressing, _garmet, _category variables from your models.

Hope this helps.


Try: attachment.instance.file_owner.class.downcase in your defined interpolation.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜