2017-09-28 3 views
0


여러 이미지를 내 시스템에 업로드하려고합니다. 나는 그것을 처리하기 위해 paperclip gem을 사용하고 있지만, 나는 2 가지 문제가있다.클립 클립 gem을 사용하여 여러 이미지 업로드 및 이미지 업데이트

1 - 여러 이미지를 해시로 저장하는 방법은 무엇입니까?
2 - 어떻게 이미지를 업데이트/삽입합니까?

사용자는 업데이트하려는 모든 제품()을 선택하고 업로드하려는 모든 이미지를 선택해야합니다. 제품과 이미지의 이름이 같으면 변경 내용이 저장됩니다.

이것은 내 altprod product_controller 함수입니다. 그것은 행동을 다루고 있지만, 중요한 유일한 부분은 가져 오기입니다 :

def altprod 
    case params[:commit] 
    (...) 
    when "Import" 
     slctProd = params[:selected_products] 
     slctProd.each do |prod| 
     if prod.eql? File.basename(params[:image].original_filename, ".*") 
      #Here is the problem :'(
      Product.where(code: prod).update(image: :image) 
     end 
     end 
     redirect_to products_url, notice: 'Insert/update images succeeded.' 
    end 
    end 

그리고 여기에 파일을 업로드 할 수있는 코드 : 도움 :

답변

0

에 대한

<%= form_tag altprod_products_path, multipart: true do %> 
    (...) 
    <%= file_field_tag :image, multiple: true %> 
    <%= submit_tag "Import", method: :post %> 
    <br/> 
    (...) 
<% end %> 

감사합니다 이 시도. 여기 avatar은 이미지 필드 이름입니다. 오른쪽 클립 클립 명령을 실행하여 이미지 모델을 생성했다고 생각합니다.

#Product Controller 

if params[:images] 
    params[:images].each { |image| 
    @product.create_image(avatar: image, user_id: current_user.id) 
    } 
end 

양식

<%= file_field_tag "images[]", type: :file,:required => true %> 

이미지 모델

belongs_to :product 
has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png", 
        size: { in: 0..1000.kilobytes }, 
         url: "/system/:hash.:extension", 
       hash_secret: "abc123" 
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/ 
validates_attachment :avatar, :presence => true, 

     :size => { :in => 0..5000.kilobytes} 

해시 이러한 유형의

종이 클립에 의해 생성된다

has_many :images, dependent: :destroy 

제품 모델

[#<ActionDispatch::Http::UploadedFile:0x007f90b22c5340 @tempfile=#<Tempfile:/tmp/RackMultipart20170929-7646-eqf009.jpeg>, @original_filename="job_post.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"book_image[]\"; filename=\"job_post.jpeg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007f90b22c52f0 @tempfile=#<Tempfile:/tmp/RackMultipart20170929-7646-1qhrsy8.jpeg>, @original_filename="query.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"book_image[]\"; filename=\"query.jpeg\"\r\nContent-Type: image/jpeg\r\n">] 
+1

도움 주셔서 감사합니다 :)) –

관련 문제