Carrierwave_Direct's direct_upload_form_for returns undefined method `model_name' for FileUploader:Class

て烟熏妆下的殇ゞ 提交于 2019-12-11 08:35:24

问题


I've been struggling with this for a couple of hours now. When using carrierwave_direct's direct_upload_form_for in my view, it returns me this error:

FileUploader Carrierwave Class:

class FileUploader < CarrierWave::Uploader::Base
  include CarrierWaveDirect::Uploader
end

ManualFile Model:

class ManualFile
  include Mongoid::Document
  mount_uploader :file, FileUploader

  field :name, :type => String
end

UploadController:

class UploadController < ApplicationController
    def manual_new
        @uploader = ManualFile.new.file
        @uploader.success_action_redirect = upload_edit_path
    end

    def manual_edit
        @myfile = ManualFile.new(key: params[:key])
    end
end

My View:

<%= direct_upload_form_for @uploader do |f| %>
  <%= f.file_field :file %>
  <%= f.submit %>
<% end %>

I can't understand what I'm doing wrong. I tried to follow this railscast. I'm using Ruby 1.9.3, Rails 3.2.3, Mongoid 3 with carrierwave gems pointing to github master.


回答1:


Apparently direct_upload_form_for works best with ActiveRecord. To make it work (or at least not crash) with Mongoid, I added the following lines to my file uploader.

include ActiveModel::Conversion
 extend ActiveModel::Naming

So in your case :

class FileUploader < CarrierWave::Uploader::Base
    include CarrierWaveDirect::Uploader

    include ActiveModel::Conversion
    extend ActiveModel::Naming
end


来源:https://stackoverflow.com/questions/20300603/carrierwave-directs-direct-upload-form-for-returns-undefined-method-model-name

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