【问题标题】:Paperclip::AdapterRegistry::NoHandlerError回形针::AdapterRegistry::NoHandlerError
【发布时间】:2016-12-17 17:10:51
【问题描述】:

我最近按照教程创建了我的 Heroku here,将图像直接上传到我的 Amazon S3 存储桶。

当我上传文件时,它已成功发送到我的 S3 存储桶,但是当我尝试保存图像时出现以下错误

    Paperclip::AdapterRegistry::NoHandlerError at /photos
No handler found for "//mys3bucket.s3.amazonaws.com/uploads/3452345aef45845blabla/the file name.fileExtension"

这是我认为的表格:

<%= bootstrap_form_for @photo, html: { multipart: true, class: 'directUpload', data: { 'form-data' => (@s3_direct_post.fields), 'url' => @s3_direct_post.url, 'host' => URI.parse(@s3_direct_post.url).host } } do |f| %>

    <p>
      <%= f.text_field :title %>
    </p>

    <p>
      <%= f.file_field :image %>
    </p>

    <%= f.submit 'Upload', class: 'btn btn-success' %>

  <% end %>

这是我在控制器中的创建方法:

  def create
    if can? :create, Photo
      @photo = Photo.new(photo_params)
      if @photo.save
        redirect_to photos_path
      else
        render 'new'
      end
    else
      redirect_to root_path
    end
  end

      private

  def photo_params
    params.require(:photo).permit(:image, :title)
  end

  def set_s3_direct_post
    @s3_direct_post = S3_BUCKET.presigned_post(key: "uploads/#{SecureRandom.uuid}/${filename}", success_action_status: '201', acl: 'public-read')
  end

这是我的模型:

    class Photo < ActiveRecord::Base
    has_attached_file :image
    belongs_to :article

    validates :title, presence: true
    validates :image,
        attachment_content_type: { content_type: /\Aimage\/.*\Z/ },
        attachment_size: { less_than: 5.megabytes }

end

这是我的全部repository

我知道出于某种原因 Paperclip 不知道如何处理 URL,但我不知道如何解决这个问题。我尝试在我的数据库中的照片表中添加一个新列,并将 URL 保存到该列。当然,这非常有效,因为没有涉及回形针。任何帮助将不胜感激。

【问题讨论】:

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


    【解决方案1】:

    您是否尝试将 s3_host_name 添加到您的配置中?

    喜欢:

    config.paperclip_defaults = {
     storage: :s3,
     s3_host_name: "s3-ap-northeast-1.amazonaws.com", #example
     s3_region: ENV['AWS_REGION'],
     s3_credentials: {
        bucket: ENV['S3_BUCKET_NAME'],
        access_key_id: ENV['AWS_ACCESS_KEY_ID'],
        secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
      }
    }
    

    【讨论】:

    • 谢谢,但我有你在 development.rb 中发布的内容,但我仍然遇到同样的错误。
    【解决方案2】:

    我似乎已经找到了答案。我认为这不是最好的方法,因为我基本上绕过了回形针,但在这一点上无论如何。

    我将模型和数据库更改为只接受 url 字符串而不是带有以下代码的附件:

    /models/photo.rb

    class Photo < ActiveRecord::Base
        belongs_to :article
        validates :title, presence: true
        validates :image_url, presence: true, length: { minimum: 5 }
    end
    

    第一次迁移

    class RemoveAttatchedImageFromPhotos < ActiveRecord::Migration
      def change
        remove_column :photos, :attached_image_file_name
        remove_column :photos, :attached_image_content_type
        remove_column :photos, :attached_image_file_size
        remove_column :photos, :attached_image_updated_at
      end
    end
    

    第二次迁移

     class AddImageUrlToPhoto2 < ActiveRecord::Migration
          def change
            add_column :photos, :image_url, :string
          end
        end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-01
      相关资源
      最近更新 更多