问题
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
.
Here is the relevant code:
app/uploaders/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 %>
回答1:
I asked a similar question that was returning a different error here.
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.
来源:https://stackoverflow.com/questions/19386581/cant-save-image-to-database-using-carrierwave-direct