Can't save Image to database using CarrierWave Direct

杀马特。学长 韩版系。学妹 提交于 2019-12-24 01:44:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!