回形针自定义:path和:url [英] paperclip custom :path and :url

查看:106
本文介绍了回形针自定义:path和:url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用回形针为has_attached_file定制:path和:url选项时遇到一些问题:

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

我有一个名为"Asset"的多态类,该类具有:

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

插值效果很好,但是我想根据file_owner_type自定义路径和url

The interpolations works well but I want to custom the 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

感谢您的帮助

编辑 : 我想我没有正确解释自己.我已经创建了插值并且效果很好.

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"

我想把它放进去

"/attachments/user_x/something_style.jpg".

谢谢

推荐答案

URL中类似以下内容:

Something like this in the url:

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

然后在插值中:

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

请注意,您需要从模型中设置_user,_dressing,_garmet,_category变量.

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

希望这会有所帮助.

这篇关于回形针自定义:path和:url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆