Paperclip::AdapterRegistry::NoHandlerError
Paperclip::AdapterRegistry::NoHandlerError
我最近按照创建 Heroku here 的教程设置了直接将图像上传到我的 Amazon S3 存储桶 here。
当我上传文件时,它已成功发送到我的 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 但我不知道如何解决这个问题。我尝试向数据库中的照片 table 添加一个新列,然后将 URL 保存到该列。这当然很完美,因为 Paperclip 没有参与其中。任何帮助将不胜感激。
您是否尝试过将 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']
}
}
看来我找到了答案。我不认为这是最好的方法,因为我基本上绕过了回形针,但在这一点上无论如何。
我更改了我的模型和数据库以仅接受 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
我最近按照创建 Heroku here 的教程设置了直接将图像上传到我的 Amazon S3 存储桶 here。
当我上传文件时,它已成功发送到我的 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 但我不知道如何解决这个问题。我尝试向数据库中的照片 table 添加一个新列,然后将 URL 保存到该列。这当然很完美,因为 Paperclip 没有参与其中。任何帮助将不胜感激。
您是否尝试过将 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']
}
}
看来我找到了答案。我不认为这是最好的方法,因为我基本上绕过了回形针,但在这一点上无论如何。
我更改了我的模型和数据库以仅接受 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