2014-12-27 4 views
1

나는 종이 클립을 사용하여 파일을 첨부하고 있습니다.클립 - 동적 사용 : 경로

필자의 경우, 각 문서에 첨부 파일을 저장하고 싶습니다. 나는 document_srl을 위해 관련 경로 동적을 생성 할 has_attached_file에 대한

class Attachment < ActiveRecord::Base 
    self.table_name = 'attachments' 
    self.primary_key = 'srl' 

    @@document_srl 

    validates :document_srl, 
      :presence => true, 
      :numericality => { only_integer: true }, 
      allow_nil: false 

    has_attached_file :attached, 
        :path => :save_path 
    validates_attachment_content_type :attached, :content_type => /\Aimage\/.*\Z/ 

    def save_path 
    ":attachment/#{@@document_srl}/:id/:style/:filename" 
    end 
end 

처럼

그래서, 내 클립 모델 클래스 보인다. (이 모델의 인스턴스를 만들 때 document_srl의 값을 설정합니다) 어떻게해야합니까?

답변

3

이 작품에는 Paperclip.interpolates를 사용할 수 있습니다.

class Attachment < ActiveRecord::Base 
    self.table_name = 'attachments' 
    self.primary_key = 'srl' 

    validates :document_srl, 
      :presence => true, 
      :numericality => { only_integer: true }, 
      allow_nil: false 

    has_attached_file :attached, 
        :path => ":attachment/:document_srl/:id/:style/:filename" 
    validates_attachment_content_type :attached, :content_type => /\Aimage\/.*\Z/ 

    Paperclip.interpolates :document_srl do |attachment, style| 
    attachment.instance.document_srl 
    end 
end 
+0

사용하려고했지만 사용하지 않으려 고했지만 컨트롤러와 모델이 있습니다. 경로 동적을 어떻게 만들 수 있는지 설명해 주시겠습니까? 내가 매개 변수로 컨트롤러에서 폴더 경로를 통과하려. –

+0

이 링크에 대한 추가 정보 https://github.com/thoughtbot/paperclip/wiki/Interpolations –