1

美好的一天。
我有導軌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 ...] 

問題
善待,請告訴我,我怎麼能提取合同編號:1

model/contract_files.rb  
     has_attached_file :data, 
     :url => "/assets/paperclip/:contract_id/:filename" 

示例我想要獲取的信息
在http // localhost:3000/contracts/1上獲取此類文件的路徑: 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

非常感謝您的幫助。

回答

1

如果我能說清楚,你可以這樣來做:

Contract.find(params[:id]).contract_files.map { |cf| cf.data.url } 
# Dont't forget to handle nonexistent id 

UPD

放置contract_id的網址,你應該恢復默認回形針:url和:路徑參數是這樣的:

#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 
end 
+0

1.9.2p290:007 cont.contract_files.map {| cf | cf.data.url} => [「/assets/paperclip/data/88/original/download.jpg?1328369668」]但你可以看到仍然沒有contract.id – itsnikolay 2012-02-04 17:12:48

+0

Getiing錯誤http://img40.imageshack .us/img40/836/screenshotat20120204212.png – itsnikolay 2012-02-04 17:33:28

+0

ContractFile定義中是否有belongs_to:contract指令? – icanhazbroccoli 2012-02-04 17:36:40