无法将ActionDispatch :: Http :: UploadedFile转换为ActiveAdmin中的字符串

时间:2014-10-06 11:15:15

标签: file upload activeadmin

我在这件事上花了一天半的时间,似乎无法找到问题所在。我非常感谢你的意见。

所以,我必须上传一个文件并将其存储在public / documents /中,并存储它的路径以及它为此目的制作的db表中的名称。 我首先将其作为前端的一个功能处理,并且工作正常(上传的文件和表中创建的记录)。

接下来,我尝试在ActiveAdmin中实现这一点,这就是我所有痛苦开始的地方。

这是我到目前为止所拥有的:

DocumentsController创建方法:

def create
        @static_pages = StaticPage.all
        @document = Document.new(doc_params)
        @uploaded_document = @document.upload(doc_params)
        if @uploaded_document
            @document.location = @uploaded_document[:loc]
            @document.doc_type = @uploaded_document[:doc_type]
            if @document.save
                flash[:notice] = "Document uploaded successfully"
                redirect_to admin_documents_path
            end
        else
            @document.errors.add(attr, "There was a problem uploading the file")
        end
    end

    private 

    def doc_params
        params.require(:document).permit :name, :location, :doc_type, :static_page_id
    end

使用上面使用的上传方法的文档模型:

class Document < ActiveRecord::Base

    belongs_to :static_page

    validates :name, presence: true, uniqueness: true
    validates :location, presence: true, uniqueness: true
    validates :doc_type, presence: true
    validates :static_page, presence: true

    def upload(doc_params)
        # Take upload from params[:file] and store it somehow...
    # Optionally also accept params[:hint] and consume if needed
    dirname = 'documents'
    name = doc_params[:location].original_filename #doc_params[:location].original_filename
    type = name.split('.').last

      Dir.mkdir("public/#{dirname}") unless Dir.exists?("public/#{dirname}")
      filepath = "public/#{dirname}"

    path = File.join(filepath, name)
    File.open(path, "wb") { |f| f.write(doc_params[:location].read) } 
    path = dirname + '/' + name
    document = { loc: path, doc_type: type }
    end
end

以及Active Admin资源定义:

ActiveAdmin.register Document do
    #actions :all, :except => [:destroy, :new, :edit]

    index do 
        column :name, sortable: :name do |document|
            link_to document.name, admin_document_path(document)
        end
        column :doc_type, sortable: :doc_type do |document|
            link_to document.doc_type, admin_document_path(document)
        end
        column :static_page, sortable: :static_page do |document|
            link_to document.static_page.title, admin_document_path(document)
        end
        column :created_at, sortable: :created_at do |document|
            link_to document.created_at, admin_document_path(document)
        end
    end

    form :html => { :enctype => "multipart/form-data" } do |f|
    f.inputs  :multipart => true do 
      f.input :name
      f.input :location, :as => :file
      f.input :static_page_id, :label => 'Parent Static Page', :as => :select, :collection => StaticPage.all.map{|sp| ["#{sp.title}", sp.id]}
    end
    f.actions
  end

  show do |form|
    attributes_table do
      row :id
      row :name
      row :location
      row :type
      row :static_page
    end
  end

        permit_params :name, :static_page_id, :location
end

当我尝试从ActiveAdmin页面上传文件时,它显示以下错误消息:

can't cast ActionDispatch::Http::UploadedFile to string

Rails.root: /home/sly/ruby/fletcher
Application Trace | Framework Trace | Full Trace

activerecord (4.1.6) lib/active_record/connection_adapters/abstract/quoting.rb:76:in `type_cast'
activerecord (4.1.6) lib/active_record/connection_adapters/postgresql/quoting.rb:136:in `type_cast'
activerecord (4.1.6) lib/active_record/validations/uniqueness.rb:64:in `build_relation'
activerecord (4.1.6) lib/active_record/validations/uniqueness.rb:19:in `validate_each'
activemodel (4.1.6) lib/active_model/validator.rb:167:in `block in validate'
activemodel (4.1.6) lib/active_model/validator.rb:164:in `each'
activemodel (4.1.6) lib/active_model/validator.rb:164:in `validate'
activesupport (4.1.6) lib/active_support/callbacks.rb:447:in `public_send'
activesupport (4.1.6) lib/active_support/callbacks.rb:447:in `block in make_lambda'
activesupport (4.1.6) lib/active_support/callbacks.rb:184:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:184:in `block in simple'
activesupport (4.1.6) lib/active_support/callbacks.rb:185:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:185:in `block in simple'
activesupport (4.1.6) lib/active_support/callbacks.rb:185:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:185:in `block in simple'
activesupport (4.1.6) lib/active_support/callbacks.rb:185:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:185:in `block in simple'
activesupport (4.1.6) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:86:in `run_callbacks'
activemodel (4.1.6) lib/active_model/validations.rb:376:in `run_validations!'
activemodel (4.1.6) lib/active_model/validations/callbacks.rb:111:in `block in run_validations!'
activesupport (4.1.6) lib/active_support/callbacks.rb:82:in `run_callbacks'
activemodel (4.1.6) lib/active_model/validations/callbacks.rb:111:in `run_validations!'
activemodel (4.1.6) lib/active_model/validations.rb:317:in `valid?'
activerecord (4.1.6) lib/active_record/validations.rb:70:in `valid?'
activerecord (4.1.6) lib/active_record/validations.rb:77:in `perform_validations'
activerecord (4.1.6) lib/active_record/validations.rb:51:in `save'
activerecord (4.1.6) lib/active_record/attribute_methods/dirty.rb:21:in `save'
activerecord (4.1.6) lib/active_record/transactions.rb:268:in `block (2 levels) in save'
activerecord (4.1.6) lib/active_record/transactions.rb:329:in `block in with_transaction_returning_status'
activerecord (4.1.6) lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
activerecord (4.1.6) lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
activerecord (4.1.6) lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
activerecord (4.1.6) lib/active_record/transactions.rb:208:in `transaction'
activerecord (4.1.6) lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
activerecord (4.1.6) lib/active_record/transactions.rb:268:in `block in save'
activerecord (4.1.6) lib/active_record/transactions.rb:283:in `rollback_active_record_state!'
activerecord (4.1.6) lib/active_record/transactions.rb:267:in `save'
/home/sly/.rvmruby (2.1.2) bundler/gems/activeadmin-6344d2924517/lib/active_admin/resource_controller/data_access.rb:158:in `block in save_resource'
activesupport (4.1.6) lib/active_support/core_ext/object/try.rb:45:in `call'
activesupport (4.1.6) lib/active_support/core_ext/object/try.rb:45:in `public_send'
activesupport (4.1.6) lib/active_support/core_ext/object/try.rb:45:in `try'
/home/sly/.rvmruby (2.1.2) bundler/gems/activeadmin-6344d2924517/lib/active_admin/callbacks.rb:79:in `block (2 levels) in define_active_admin_callbacks'
/home/sly/.rvmruby (2.1.2) bundler/gems/activeadmin-6344d2924517/lib/active_admin/resource_controller/data_access.rb:157:in `save_resource'
/home/sly/.rvmruby (2.1.2) bundler/gems/activeadmin-6344d2924517/lib/active_admin/resource_controller/data_access.rb:147:in `block in create_resource'
activesupport (4.1.6) lib/active_support/core_ext/object/try.rb:45:in `call'
activesupport (4.1.6) lib/active_support/core_ext/object/try.rb:45:in `public_send'
activesupport (4.1.6) lib/active_support/core_ext/object/try.rb:45:in `try'
/home/sly/.rvmruby (2.1.2) bundler/gems/activeadmin-6344d2924517/lib/active_admin/callbacks.rb:79:in `block (2 levels) in define_active_admin_callbacks'
/home/sly/.rvmruby (2.1.2) bundler/gems/activeadmin-6344d2924517/lib/active_admin/resource_controller/data_access.rb:146:in `create_resource'
inherited_resources (1.4.1) lib/inherited_resources/actions.rb:33:in `create'
actionpack (4.1.6) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.1.6) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.6) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.6) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.6) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.6) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.6) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.6) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.6) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.6) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.6) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.6) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.6) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.6) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.6) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.6) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.6) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:82:in `call'
actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.6) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.6) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.6) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:678:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.6) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.6) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.6) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.6) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.6) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.6) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.6) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.6) lib/rails/engine.rb:514:in `call'
railties (4.1.6) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/home/sly/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/home/sly/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/home/sly/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'

Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"rUZ0xBtYmYu+HdpZQalOXqh5wZdgiKHm8fuTuUKu6o4=",
 "document"=>{"name"=>"Sample new Doc",
 "location"=>#<ActionDispatch::Http::UploadedFile:0xc01ac64 @tempfile=#<Tempfile:/tmp/user/1000/RackMultipart20141006-13048-9uig4j>,
 @original_filename="SvetoslavBlyahov_portfolio.pdf",
 @content_type="application/force-download",
 @headers="Content-Disposition: form-data; name=\"document[location]\"; filename=\"SvetoslavBlyahov_portfolio.pdf\"\r\nContent-Type: application/force-download\r\n">,
 "static_page_id"=>"1"},
 "commit"=>"Create Document",
 "locale"=>"en"}

我尝试了很多不同的方法来尝试调查我不知道还能做什么的问题。从这个功能的角度来看我们的AA而不是,我很确定它来自Active Admin。但我不知道如何解决它。假设它是这一部分:

  

f.input:location,:as =&gt; :文件

as:位置&在DB中的字段类型是:string所以我可以保存存储上保存文件的路径。然而,我没有其他办法让字段允许文件上传。

任何建议都将受到高度赞赏。

谢谢。

2 个答案:

答案 0 :(得分:1)

经过几天的努力,我决定写在GitHub的AA存储库的支持部分。感谢一位非常乐于助人且耐心的人(我猜AA的开发者社区的一部分),我的问题得到了解决。

这是Github中主题的链接。 https://github.com/activeadmin/activeadmin/issues/3476

答案 1 :(得分:0)

使用CarrierWave且模型中没有mount_uploader时将显示此错误。

模型正在尝试将上传的文件另存为指定列中的字符串。 CarrierWave会将其转换为文件位置和url字符串。

只需将其添加到模型中

mount_uploader :column_name, UploaderClass