0

好的,所以我有这个模型...

class Asset < ActiveRecord::Base
  attr_accessible :asset_file_name, :lesson_id, :attachment

  has_attached_file :attachment,
    :url => "/attachments/:id/:basename.:extension",
    :path => ":rails_root/public/attachments/:id/:basename.:extension"

  validates_presence_of :asset_file_name
  validates_attachment_presence :attachment
end

(也有一个模型Lesson

我想将附件保存到/attachments/:lesson_id/:basename.:extension. 这不是:id资产的,而是 :lesson_id。当我这样做时,目录实际上只是命名为:lesson_id. 有人知道如何获得实际的lesson_id吗?我也试过了@asset.lesson_id

4

1 回答 1

1

您必须在回形针中添加自定义插值器。这最好在初始化程序中或与模型分离的地方完成

Paperclip.interpolates('lesson_id') do |attachment, style|
  attachment.instance.lesson_id
end

之后,您:lesson_id将成为实际的object.lesson_id

于 2012-07-11T15:33:08.707 回答