无法使用CarrierWave Direct将图像保存到数据库

时间:2013-10-15 16:44:41

标签: ruby-on-rails carrierwave

我正在使用CarrierWave Direct和Sidekiq在后台将图像上传到Amazon S3。正在按预期上传图像,但我似乎无法将图像记录添加到数据库中。当我提出错误时,我会得到filename can't be blank

以下是相关代码:

应用/上传/ image_uploader.rb

class ImageUploader < CarrierWave::Uploader::Base

  include CarrierWaveDirect::Uploader

  include CarrierWave::MimeTypes
  process :set_content_type

end

应用/控制器/管理/ 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

应用/模型/ 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

应用/视图/管理/图像/ new.html.erb

<%= direct_upload_form_for @uploader do |f| %>
  <p><%= f.file_field :filename %></p>
  <p><%= f.submit "Upload Image" %></p>
<% end %>

1 个答案:

答案 0 :(得分:0)

我问了一个类似的问题,它返回了另一个错误here

两种情况下的解决方案都是一样的。 carrierwave_direct使用&#34; filename&#34;作为一种方法。因此,在数据库filename中命名列是导致此问题的原因。