2016-08-04 5 views
1

레일 클립을 사용하고 클립을 사용하여 AWS S3 버킷에 파일을 업로드하고 있습니다. 내 모델에서 , 나는 다음과 같은 방법으로 같은 구성 :클립/레일을 사용하여 파일을 저장하는 경로를 변경하십시오.

class File < ApplicationRecord 
    has_attached_file :attachment, 
           :url => "/sample_pdf/:basename.:extension", 
           :path => "/sample_pdf/:basename.:extension" 

    validates_attachment :attachment, 
             :content_type => { 
               :content_type => 
                 ["application/pdf"] 
             } 
end 

내가 직면하고 문제는 가끔 "/sample_pdf/:basename.:extension"에 파일을 업로드해야하고 때로는 내가 "/another_pdf_folder/:basename.:extension"을 다음과 같이 다른 경로로 업로드 할 필요가있다.

파일을 저장하는 경로를 변경하는 방법이 필요한지 여부는 잘 모르겠습니다.

감사합니다.

답변

0

경로를 모델의 메소드를 통해 동적으로 설정할 수 있습니다.

class File < ApplicationRecord 
    has_attached_file :attachment, 
       :url => "/sample_pdf/:basename.:extension", 
       :path => :attachment_dynamic_path, 
       validates_attachment :attachment, 
       :content_type => { 
           :content_type => ["application/pdf"] 
           } 
    def attachment_dynamic_path 
    condition ? "/sample_pdf/:basename.:extension" : "/another_pdf_folder/:basename.:extension" 
    end 
end 
+0

나는 조건에 익숙하지 않습니다. 어느 길로 가야합니까? 하나를 선택할 수있는 방법이 있습니까? – JoHksi

+0

경로 중 하나에 파일을 저장하려는 조건을 가지고 있지 않습니까? – titan

+0

그래서 두 개의 다른 모델이 있습니다 : ServiceA, ServiceB. 각 모델에는 여러 파일이 있으므로 각 서비스 아래에 여러 파일을 업로드 할 수 있습니다. 내가 사용하는 서비스에 따라 경로가 변경되어야합니다. 하지만'File' 모델 수준에서 파일을 업로드 할 서비스를 확인하는 방법을 모르겠습니다. – JoHksi