【发布时间】:2012-03-21 07:21:35
【问题描述】:
我使用附件模型作为多态关联。如何根据关联更改路径和 url 参数。
附件模型
class Attachment < ActiveRecord::Base
belongs_to :user
belongs_to :attachable, :polymorphic => true
has_attached_file :attachment,
:url => "/attachments/:id/:basename.:extension",
:path => ":rails_root/public/attachments/:id/:basename.:extension",
:default_url => "/attachments/original/no-file.txt"
end
项目模型
class Project < ActiveRecord::Base
...
has_many :attachments, :as => :attachable, :dependent => :destroy
end
客户
class Client < ActiveRecord::Base
...
has_many :attachments, :as => :attachable, :dependent => :destroy
end
以下路径文件保存正常。
:path => ":rails_root/public/attachments/:id/:basename.:extension",
但我需要根据关联保存文件,为此如何将参数传递给“路径”。 'attachable_type' 定义了上传文件属于哪个关联
/attachments/project/
/attachments/client/
【问题讨论】: