Questifer
Questifer

Reputation: 1123

Uploading two different images to one model using paperclip

In my rails 4 app I have a model called ListingInformationForm. In this form I want the user to upload a logo and a picture of their factory. I am using the paperclip gem with AWS S3. Currently, when I submit the form, it is saving the AWS S3 link of the second image (loan_image) to both the logo and loan_image.

listing_information_form.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"
  # Assocations
  belongs_to :business
end

new.html.erb

<%= form_for @listing_information_form, url: business_listing_information_form_path(@user), :html => { :multipart => true } do |f| %>
    <%= f.hidden_field :business_id, :value => @user.id %>
    <%= f.hidden_field :loan_id, :value => @loan_id %>
    <div class="reg-header">
        <h2>Tell us about your business.</h2>
        <p>The application will only take 10 minutes of your time!</p>
    </div>
    <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(:business_id, :loan_id, :logo, :loan_image)
end

Upvotes: 0

Views: 303

Answers (1)

cthulhu
cthulhu

Reputation: 3726

I guess it's because both 'path' attributes, for logo and loan_image, are the same:

:path => "images/:class/:id.:style.:extension"

Change them to:

:path => "logos/:id.:style.:extension" 

for logos

and

:path => "loan_images/:id.:style.:extension"

for loan images

Upvotes: 1

Related Questions