简体   繁体   English

使用s3和Rails应用上的回形针上传两个图像

[英]Uploading two images using s3 and paperclip on rails app

I have a model called ListingInformationFrom. 我有一个名为ListingInformationFrom的模型。 I've added two paperclip migrations to it: logo and loan_image. 我在其中添加了两个回形针迁移:徽标和贷款图像。 When I upload two different images in the form, the URLs being saved show the same image. 当我在表单中上传两个不同的图像时,保存的URL显示相同的图像。 Can someone shed some light on how I can upload two separate images to one model? 有人可以阐明我如何将两个单独的图像上传到一个模型吗?

ListingInformationForm.rb ListingInformationForm.rb

    class ListingInformationForm < ActiveRecord::Base
        # Image uploading
        has_attached_file :logo,
                          :styles => { :medium => "300x300>", :thumb => "100x100>" },
                          :storage => :s3, 
                          :url => ":s3_domain_url",
                          :path => "images/:class/:id.:style.:extension"
        has_attached_file :loan_image,
                          :styles => { :medium => "300x300>", :thumb => "100x100>" },
                          :storage => :s3, 
                          :url => ":s3_domain_url",
                          :path => "images/:class/:id.:style.:extension"
    end

The form includes the following: 该表格包括以下内容:

      <%= form_for @listing_information_form, url: business_listing_information_form_path(@user), :html => { :multipart => true } do |f| %>
      <div class="row">
        <div class="form-group">
            <div class="col-md-5">
                <%= f.label :logo, :class => "control-label required" %>
            </div>
            <div class="col-md-7">
                <%= f.file_field :logo %>
            </div>       
        </div>
    </div>
    <div class="row">
        <div class="form-group">
            <div class="col-md-5">
                <%= f.label :loan_image, :class => "control-label required" %>
             </div>
             <div class="col-md-7">
                 <%= f.file_field :loan_image %>
             </div>       
        </div>
    </div>
    <% end %>

listing_information_forms_controller.rb listing_information_forms_controller.rb

def listing_information_form_params
    params.require(:listing_information_form).permit(:loan_id, :logo, :loan_image)
end 

s3 Config in development.rb development.rb中的s3 Config

# s3 config
  config.paperclip_defaults = {
  :storage => :s3,
  :s3_credentials => {
    :bucket => ENV['AWS_BUCKET'],
    :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
    :secret_access_key => ENV['AWS_SECRET_KEY_ID']
    }
  }

Please make sure you have added :html => { :multipart => true } in your Form. 请确保在表单中添加了:html => { :multipart => true } like 喜欢

<%= form_for @listinformation, :url => listinformation_path, :html => { :multipart => true } do |form| %>
  <%= form.file_field :logo %>
  <%= form.file_field :logo_image%>
<% end %>

if you are using Rails 4.x , please make sure you have define it on strong parameters. 如果您使用的是Rails 4.x,请确保已在强参数上对其进行了定义。

def listinformation_params
  params.require(:listinformation).permit(:logo, :logo_image)
end

Please paste your errors if still you face difficulty 如果仍然遇到困难,请粘贴错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM