简体   繁体   中英

Uploading two images using s3 and paperclip on rails app

I have a model called 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. Can someone shed some light on how I can upload two separate images to one model?

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

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

s3 Config in development.rb

# 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. 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.

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

Please paste your errors if still you face difficulty

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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