具有id belongs_to的回形针自定义路径

时间:2012-02-07 09:38:31

标签: ruby-on-rails-3 paperclip

我在项目中使用了paperclip。

有关模型看起来像这样:

class Asset < ActiveRecord::Base

  has_paper_trail                              # Track model with paper_trail
  has_attached_file :image, :styles => { :medium => "300x300>"}, 
  :path => ":rails_root/public/attachments/project_#{:project_id}/ressources/:basename_:style.:extension"

  belongs_to :project

end

如您所见,我希望在我的图片路径中拥有相关项目的ID,我该如何制作?

1 个答案:

答案 0 :(得分:6)

试试这个

has_attached_file :image, :styles => { :medium => "300x300>"}, 
 path => ":rails_root/public/attachments/:project_id/ressources/:basename_:style.:extension"

然后写

Paperclip.interpolates :project_id do |attachment, style|
    "project_#{attachment.instance.project_id}"
  end