日安。
我有rails 3.1。和宝石纸夹
在我管理公司合同的申请中:
MODEL
model/contract.rb
has_many :contract_files
model/contract_file.rb
has_attached_file :data
CONSOLE
Loading development environment (Rails 3.1.0)
1.9.2p290 :001 > cont = Contract.first
Contract Load (0.1ms) SELECT "contracts".* FROM "contracts" LIMIT 1
=> #Contract id: 1, organization: "Com.org", and etc ....
1.9.2p290 :002 > cont.contract_files
ContractFile Load (0.2ms) SELECT "contract_files".* FROM "contract_files"
WHERE "contract_files"."contract_id" = 1
=> #[ContractFile id: 88, caption: "asdf", and etc ...]发问
仁慈点,请告诉我,我如何提取合同id: 1 in
model/contract_files.rb
has_attached_file :data,
:url => "/assets/paperclip/:contract_id/:filename"举个例子我想要什么
On http//localhost:3000/contracts/1 get such files pathes: http//localhost:3000/contracts/1/assets/paperclip/1/XXX.pdf http//localhost:3000/contracts/1/assets/paperclip/1/XXY.pdf http//localhost:3000/contracts/1/assets/paperclip/1/XXZ.pdf非常感谢你的帮助。
发布于 2012-02-04 16:58:50
如果我弄清楚你可能会这样做:
Contract.find( params[:id] ).contract_files.map { |cf| cf.data.url }
# Dont't forget to handle nonexistent id更新
要将contract_id放在url中,您应该以这样的方式恢复默认的回形针:url和:path参数:
#model/contract_file.rb
has_attached_file :data, :path => "public/contracts/:parent_id/assets/paperclip/:id.:extension", :url => "/contracts/:parent_id/assets/paperclip/:id.:extension"
Paperclip.interpolates :parent_id do |a, s|
a.instance.contract.id
endhttps://stackoverflow.com/questions/9142476
复制相似问题