【问题标题】:Paperclip custom path with id belongs_to回形针自定义路径,id 为 belongs_to
【发布时间】:2012-02-07 18:13:25
【问题描述】:

我在一个项目中使用回形针。

相关模型如下所示:

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,我该怎么做?

【问题讨论】:

    标签: ruby-on-rails-3 paperclip


    【解决方案1】:

    试试这个

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-21
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多