阅读背景:

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

来源:互联网 

I am trying to get direct upload to work on Heroku using jquery fileupload but it shows the error in the title.

我试图使用jquery fileupload直接上传到Heroku上工作,但它显示标题中的错误。

I really think I did follow Heroku's piece of documentation thoroughly.

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

The part where it writes to change the view generated by the scaffold from

它写入的部分用于更改脚手架生成的视图

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

to

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

confuses me since when generating the scaffold we wrote avatar_url:string and our view now wants a file. And since there is no mention on what to do in the controller's create action, I kept the one generated by the scaffold. For the record here is my create action :

让我感到困惑,因为在生成脚手架时我们写了avatar_url:string,我们的视图现在需要一个文件。而且由于没有提及控制器的创建动作中的操作,我保留了脚手架生成的那个。这里的记录是我的创建动作:

  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

Some important information may be lacking there or it is totally possible that I am too much of a beginner to understand implicitly suggested changes too...

一些重要的信息可能缺乏,或者完全有可能我太初学者也无法理解隐含建议的变化......

Either way, I would highly appreciate more detail on what to do to get this direct upload tutorial to work.

无论哪种方式,我都非常感谢有关如何使这个直接上传教程工作的更多细节。

Thanks again! Dan

再次感谢!担

The full trace:

完整的痕迹:

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"}

1 个解决方案

#1


0  

Honestly, I would recommend using Carrierwave or Paperclip to handle image uploads with Rails.

老实说,我建议使用Carrierwave或Paperclip来处理使用Rails上传的图像。

The Heroku tutorial refers to a client implementation of file uploads, which I would imagine is not a core requirement of your application. The gems I mentioned use the server as a temporary cache, so you can specify file_field in your view and it will handle the database relationship via an AvatarUploader (or whatever kind of uploader you use).

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

You'll also need to configure Fog with one of those gems if you want to communicate with S3. I would recommend Carrierwave, its my favorite and their documentation spells out how to handle this with Rails.

如果要与S3通信,还需要使用其中一个宝石配置Fog。我会推荐Carrierwave,它是我的最爱,他们的文档阐述了如何使用Rails处理这个问题。


分享到: