使用回形针将文件上传到S3

时间:2018-06-23 19:46:17

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

我一直在尝试设置Paperclip,以允许我们将文件发送到S3。我对S3或Paperclip不太熟悉,因此我在https://devcenter.heroku.com/articles/paperclip-s3处关注了该教程,但是遇到了一些问题。据我所知,我完全按照指示进行操作,但是在附加文件(在本例中为SpringPictures_FrontSlider.jpg)后单击“提交”按钮时,我仍然收到错误消息:

“ DprojectsController#create中的Paperclip :: AdapterRegistry :: NoHandlerError”

“未找到“ SpringPictures_FrontSlider.jpg”处理程序”

并且控制器中的这一行有问题:

“ @ dproject = Dproject.new(dproject_params)”

我想知道怎么回事。感谢您的协助!这也是我的所有代码-只是因为我不确切知道问题出在哪里。我使用的支架是“ dprojects”,我将附件迁移到了简单的“附件”。

宝石文件:

source 'https://rubygems.org'
gem 'aws-sdk', '~> 1.6.0'
gem "paperclip", "~> 4.3"

config / environment / production.rb:

config.paperclip_defaults = {
  :storage => :s3,
  :s3_credentials => {
    :bucket => ENV['S3_BUCKET_NAME'],
    :access_key_id => ENV['ACCESS_KEY'],
    :secret_access_key => ENV['SECRET_KEY']
    }
  }

dproject.rb(模型):

class Dproject < ActiveRecord::Base
has_attached_file :attachment, styles: {
        thumb: '100x100>',
        square: '200x200#',
        medium: '300x300>'
    }

    validates_attachment_content_type :attachment, :content_type => /\Aimage\/.*\Z/

迁移:

class AddAttachementToDprojects < ActiveRecord::Migration
  def self.up
    add_attachment :dprojects, :attachment
  end

  def self.down
    remove_attachment :dprojects, :attachment
  end
end

_formNew.html.erb(视图):

<%= form_for @dproject, :html => {:multipart => true} do |f| %>
    <div class="field">
      <%= f.file_field :attachment %>
      <%= f.submit class: "btn btn-primary", id: "submit" %>
    </div>
<% end %>

dprojects_controller.rb(控制器):

def create
    @dproject = Dproject.new(dproject_params)

    if @dproject.save
      redirect_to @dproject, notice: 'File successfully attached'
    else
      render action: 'new'
    end
end

private

def dproject_params
      params.require(:dproject).permit(:scode, :description, :requested_by, :assigned_to, :completed_at, :order_num, :contact, :contact_email, :ptype, :due_date, :must_date, :print_date, :proofs_out, :proofs_in, :status, :delivery_type, :route, :delivery_date, :tracking_number, :shipping_instructions, :invoicing, :delivered, :printing_instructions, :project_changes, :invoice_quantity, :invoice_price, :project_path, :attachment)
    end
end

config / initializers / paperclip.rb:

Paperclip::Attachment.default_options[:storage] = :s3
Paperclip::Attachment.default_options[:s3_protocol] = 'https'
Paperclip::Attachment.default_options[:s3_credentials] = 
  { :bucket => ENV['S3_BUCKET_NAME'],
    :access_key_id => ENV['ACCESS_KEY'],
    :secret_access_key => ENV['SECRET_KEY'] }

0 个答案:

没有答案