繁体   English   中英

在遵循Heroku关于如何直接上传到AWS S3的教程之后,无法将ActionDispatch :: Http :: UploadedFile转换为字符串

[英]can't cast ActionDispatch::Http::UploadedFile to string after following Heroku's tutorial on how to upload directly to aws s3

我正在尝试使用jquery fileupload直接上传以在Heroku上工作,但标题中显示错误。

我真的认为我确实完全遵循了Heroku的文档

它编写以更改由脚手架生成的视图的部分

<div class="field">
  <%= f.label :avatar_url %><br>
  <%= f.text_field :avatar_url %>
</div>

<div class="field">
  <%= f.label :avatar_url %><br>
  <%= f.file_field :avatar_url %>
</div>

使我感到困惑,因为在生成支架时,我们编写了avatar_url:string,而我们的视图现在需要一个文件。 而且由于没有提及控制器的create动作该怎么做,因此我保留了由脚手架生成的动作。 作为记录,这是我的create动作:

  def create
    @user = User.new(user_params)

      respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

那里可能缺少一些重要的信息,或者我完全是一个太初学者,也无法理解隐式建议的更改...

无论哪种方式,我都非常赞赏如何使此直接上载教程正常工作的更多详细信息。

再次感谢!

完整的跟踪:

activerecord (4.1.8) lib/active_record/connection_adapters/abstract/quoting.rb:81:in `type_cast'
activerecord (4.1.8) lib/active_record/connection_adapters/sqlite3_adapter.rb:261:in `type_cast'
activerecord (4.1.8) lib/active_record/connection_adapters/sqlite3_adapter.rb:295:in `block in exec_query'
activerecord (4.1.8) lib/active_record/connection_adapters/sqlite3_adapter.rb:294:in `map'
activerecord (4.1.8) lib/active_record/connection_adapters/sqlite3_adapter.rb:294:in `exec_query'
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/database_statements.rb:68:in `exec_insert'
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/database_statements.rb:95:in `insert'
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
activerecord (4.1.8) lib/active_record/relation.rb:64:in `insert'
activerecord (4.1.8) lib/active_record/persistence.rb:503:in `_create_record'
activerecord (4.1.8) lib/active_record/attribute_methods/dirty.rb:87:in `_create_record'
activerecord (4.1.8) lib/active_record/callbacks.rb:306:in `block in _create_record'
activesupport (4.1.8) lib/active_support/callbacks.rb:82:in `run_callbacks'
activerecord (4.1.8) lib/active_record/callbacks.rb:306:in `_create_record'
activerecord (4.1.8) lib/active_record/timestamp.rb:57:in `_create_record'
activerecord (4.1.8) lib/active_record/persistence.rb:483:in `create_or_update'
activerecord (4.1.8) lib/active_record/callbacks.rb:302:in `block in create_or_update'
activesupport (4.1.8) lib/active_support/callbacks.rb:82:in `run_callbacks'
activerecord (4.1.8) lib/active_record/callbacks.rb:302:in `create_or_update'
activerecord (4.1.8) lib/active_record/persistence.rb:103:in `save'
activerecord (4.1.8) lib/active_record/validations.rb:51:in `save'
activerecord (4.1.8) lib/active_record/attribute_methods/dirty.rb:21:in `save'
activerecord (4.1.8) lib/active_record/transactions.rb:268:in `block (2 levels) in save'
activerecord (4.1.8) lib/active_record/transactions.rb:329:in `block in with_transaction_returning_status'
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
activerecord (4.1.8) lib/active_record/transactions.rb:208:in `transaction'
activerecord (4.1.8) lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
activerecord (4.1.8) lib/active_record/transactions.rb:268:in `block in save'
activerecord (4.1.8) lib/active_record/transactions.rb:283:in `rollback_active_record_state!'
activerecord (4.1.8) lib/active_record/transactions.rb:267:in `save'
app/controllers/users_controller.rb:31:in `block in create'
actionpack (4.1.8) lib/action_controller/metal/mime_responds.rb:433:in `call'
actionpack (4.1.8) lib/action_controller/metal/mime_responds.rb:433:in `retrieve_collector_from_mimes'
actionpack (4.1.8) lib/action_controller/metal/mime_responds.rb:256:in `respond_to'
app/controllers/users_controller.rb:30:in `create'
actionpack (4.1.8) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.8) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.8) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:678: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.8) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.8) 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.8) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.8) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.8) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.8) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.8) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.8) 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.8) 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.8) lib/action_dispatch/middleware/static.rb:84:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.8) lib/rails/engine.rb:514:in `call'
railties (4.1.8) 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/dan/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/home/dan/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/home/dan/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'

Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"AKeLTFglP6aT0OFd6/Er6lITrhleJ/OZyj2FUAMcssU=",
 "user"=>{"name"=>"yuiuiu",
 "avatar_url"=>#<ActionDispatch::Http::UploadedFile:0x007ff590ce2b00 @tempfile=#<Tempfile:/tmp/RackMultipart20141215-7051-1s9e6tq>,
 @original_filename="imagetest.jpg",
 @content_type="image/jpeg",
 @headers="Content-Disposition: form-data; name=\"user[avatar_url]\"; filename=\"imagetest.jpg\"\r\nContent-Type: image/jpeg\r\n">},
 "commit"=>"Create User"}

老实说,我建议使用CarrierwavePaperclip通过Rails处理图像上传。

Heroku教程涉及文件上传的客户端实现,我想这不是您应用程序的核心要求。 我提到的gems使用服务器作为临时缓存,因此您可以在视图中指定file_field ,它将通过AvatarUploader(或您使用的任何类型的上载器)处理数据库关系。

如果要与S3通信,还需要使用其中一个gem配置Fog 我会推荐Carrierwave,它是我的最爱,他们的文档详细说明了如何使用Rails处理此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM