简体   繁体   中英

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

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

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

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 :

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

Honestly, I would recommend using Carrierwave or Paperclip to handle image uploads with 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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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