2012-02-07 34 views
3

我在項目中使用回形針。帶ID的回形針自定義路徑belongs_to

有關模型是這樣的:

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,我該怎麼做呢?

回答

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