1

안녕하세요.
난 레일 3.1 있습니다. 내가 계약 ID를 추출 할 수있는 방법, 제발 말해,
gem 클립 클립 보간

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

나는 분명히 당신이 이런 식으로 할 수 얻을 경우 URL과 : 경로 매개 변수이 방법 :

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

UPD

는 기본 클립을 복구해야 URL에 CONTRACT_ID 곳으로

#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"]하지만 알 수 있듯이 여전히 계약이 없습니다 .id – itsnikolay

+0

Getiing 오류 http : //img40.imageshack .us/img40/836/screenshotat20120204212.png – itsnikolay

+0

belongs_to : ContractFile 정의에 contract 지시어가 있습니까? – icanhazbroccoli

관련 문제