在rails app上使用s3和paperclip上传两个图像

时间:2014-11-24 17:28:46

标签: ruby-on-rails ruby-on-rails-4 amazon-s3 paperclip

我有一个名为ListingInformationFrom的模型。我已经添加了两个回形针迁移:logo和loan_image。当我在表单中上传两个不同的图像时,保存的URL显示相同的图像。有人可以说明如何将两个单独的图像上传到一个模型吗?

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

表单包含以下内容:

      <%= 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在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']
    }
  }

1 个答案:

答案 0 :(得分:0)

请确保您在表单中添加了:html => { :multipart => true }。 喜欢

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

如果您使用的是Rails 4.x,请确保您已在强参数上定义它。

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

如果您遇到困难,请粘贴您的错误