无法使用CarrierWave Direct将图像保存到数据库 [英] Can't save Image to database using CarrierWave Direct
问题描述
我正在将CarrierWave Direct与Sidekiq结合使用,以在后台将图像上传到Amazon S3。图片已按预期上传,但我似乎无法将图片记录添加到数据库中。当我提出错误时,我得到文件名不能为空 c。
I am using CarrierWave Direct with Sidekiq to upload images to Amazon S3 in the background. The images are being uploaded as expected, but I can't seem to add the image record to the database. When I raise errors, I get that filename can't be blank
.
这里是相关代码:
应用程序/上传器/image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWaveDirect::Uploader
include CarrierWave::MimeTypes
process :set_content_type
end
app / controllers / admin / images_controller.rb
def index
@uploader = Image.new.filename
@uploader.success_action_redirect = new_admin_project_image_url(@project)
render "new"
end
def new
@image = Image.new
@image.filename = params[:key]
@image.imageable = @project
unless @image.save
raise @image.errors.to_yaml
end
end
app / models / image.rb
class Image < ActiveRecord::Base
attr_accessible :imageable_id, :imageable_type, :filename
validates :filename, presence: true
validates :imageable_id, presence: true
validates :imageable_type, presence: true
belongs_to :imageable, polymorphic: true
mount_uploader :filename, ImageUploader
end
app / views / admin / images / new.html.erb
<%= direct_upload_form_for @uploader do |f| %>
<p><%= f.file_field :filename %></p>
<p><%= f.submit "Upload Image" %></p>
<% end %>
推荐答案
我问了一个类似的问题,该问题返回了另一个错误此处。
I asked a similar question that was returning a different error here.
两种情况下的解决方案相同。 carrierwave_direct
使用文件名作为方法。因此,在数据库 filename
中命名该列正是导致此问题的原因。
The solution in both cases was the same. carrierwave_direct
uses "filename" as a method. Therefore, naming the column in the database filename
is very what is causing the issue here.
这篇关于无法使用CarrierWave Direct将图像保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!