2013-10-15 61 views
2

我正在使用CarrierWave Direct和Sidekiq在後臺將圖像上傳到Amazon S3。圖像正在按預期上傳,但我似乎無法將圖像記錄添加到數據庫。當我提出錯誤時,我得到filename can't be blank無法使用CarrierWave Direct將圖像保存到數據庫

下面是相關的代碼:

應用程序/上傳/ 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 

應用/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 

應用程序/視圖/管理/圖片/ new.html.erb

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

回答

0

我問這是返回不同的錯誤here類似的問題。

兩種情況下的解決方案都是一樣的。​​使用「文件名」作爲方法。因此,命名數據庫filename中的列是造成這個問題的原因。

相關問題