0

我遇到了一个 Ruby on Rails POST 的问题,它具有以下输出:

Started POST "/550-674-359/pictures" for 127.0.0.1 at 2012-07-16 15:30:53 -0400

Processing by PicturesController#create as JSON
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"S33+XBxaGA7J+VWrr59CXKsZDk6lsI0/rBfHoYsCUUw=", "expiration_date"=>"1", "alignment"=>"center", "picture"=>{"image"=>[#<ActionDispatch::Http::UploadedFile:0x000001009a7a08 @original_filename="Sunflower.gif", @content_type="image/gif", @headers="Content-Disposition: form-data; name=\"picture[image][]\"; filename=\"Sunflower.gif\"\r\nContent-Type: image/gif\r\n", @tempfile=#<File:/var/folders/zZ/zZurKpTZFDm2y34FYgB3PLtShpg/-Tmp-/RackMultipart20120716-21348-j6977v>>]}, "url_id"=>"550-674-359"}
  Gallery Load (0.3ms)  SELECT "galleries".* FROM "galleries" WHERE "galleries"."url_id" = '550-674-359' LIMIT 1
   (0.1ms)  begin transaction
Binary data inserted for `string` type on column `image`
  SQL (0.7ms)  INSERT INTO "pictures" ("created_at", "description", "gallery_id", "gallery_url_id", "image", "order_number", "original_filename", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?)  [["created_at", Mon, 16 Jul 2012 19:30:53 UTC +00:00], ["description", nil], ["gallery_id", 28], ["gallery_url_id", "550-674-359"], ["image", "d2792dbf99.gif"], ["order_number", nil], ["original_filename", "Sunflower.gif"], ["updated_at", Mon, 16 Jul 2012 19:30:53 UTC +00:00]]
   (0.6ms)  rollback transaction
Completed 500 Internal Server Error in 24ms

TypeError (can't convert nil into String):
  app/controllers/pictures_controller.rb:60:in `create'

这是明确标记第 60 行的 create 方法:

    # POST /pictures
  # POST /pictures.json
  def create
    # Creates a picture class instance based on the json objects being passed in from 
    # Carrierwave
    p_attr = params[:picture]
    p_attr[:image] = params[:picture][:image].first if params[:picture][:image].class == Array

    # Gets the gallery vai the uuid gallery url
    @gallery = Gallery.where(:url_id => params[:url_id]).first
    # Builds the picture based on the attributes passed in above from the json
    @picture = @gallery.pictures.build(p_attr)
    # Assigns the gallery_url_id attribute
    @picture.gallery_url_id = @gallery.url_id

    # Handle the picture save according to success or not
    **LINE 60 ->**if @picture.save
      puts @picture.inspect
      respond_to do |format|
        format.html {
          # Calls the method in the model to format the json repsonse
          render :json => [@picture.to_jq_upload].to_json,
          :content_type => 'text/html',
          :layout => false
        }
        format.json {
          render :json => [@picture.to_jq_upload].to_json
        }
      end
    else
      render :json => [{:error => "custom_failure"}], :status => 304
    end
  end

以及我试图确保没有尝试将 nil 推入数据库的模型:

    class Picture < ActiveRecord::Base

  # The attributes accesible via an @picture
  attr_accessible :gallery_id, :description, :image, :gallery_url_id, :original_filename, :order_number
  belongs_to :gallery

  # Attatches the carrierwave uploader to the picture class
  mount_uploader :image, ImageUploader

  # Referenced when each picture is created, the json is formatted as the following form
  def to_jq_upload
    {

      "name" => read_attribute(:image),
      "size" => image.size,
      "url" => image.url,
      "thumbnail_url" => image.thumb.url,
      "delete_url" => id,
      "picture_id" => id,
      "delete_type" => "DELETE",
      "url_id" => read_attribute(:gallery_url_id),
      "original_filename" => read_attribute(:original_filename),
      "order_number" => testParams(read_attribute(:order_number)),
      "description" => testParams(read_attribute(:description))
    }
  end

  def testParams(id)
    if id == nil
        return ""
    else
      return id
    end

  end

end

据我所知,似乎 order_number 或 description 是此问题的根源。我认为我创建的名为 testParams 的方法会检查并相应地处理,但我想它不会。我怎样才能解决这个问题?

编辑:这里的完整堆栈跟踪...

fog (1.0.0) lib/fog/core/hmac.rb:23:in `digest'
fog (1.0.0) lib/fog/core/hmac.rb:23:in `block in setup_sha1'
fog (1.0.0) lib/fog/core/hmac.rb:15:in `call'
fog (1.0.0) lib/fog/core/hmac.rb:15:in `sign'
fog (1.0.0) lib/fog/aws/storage.rb:352:in `signature'
fog (1.0.0) lib/fog/aws/storage.rb:360:in `request'
fog (1.0.0) lib/fog/aws/requests/storage/put_object.rb:43:in `put_object'
fog (1.0.0) lib/fog/aws/models/storage/file.rb:119:in `save'
fog (1.0.0) lib/fog/core/collection.rb:50:in `create'
carrierwave (0.6.2) lib/carrierwave/storage/fog.rb:250:in `store'
carrierwave (0.6.2) lib/carrierwave/storage/fog.rb:84:in `store!'
carrierwave (0.6.2) lib/carrierwave/uploader/store.rb:59:in `block in store!'
carrierwave (0.6.2) lib/carrierwave/uploader/callbacks.rb:17:in `with_callbacks'
carrierwave (0.6.2) lib/carrierwave/uploader/store.rb:58:in `store!'
carrierwave (0.6.2) lib/carrierwave/mount.rb:345:in `store!'
carrierwave (0.6.2) lib/carrierwave/mount.rb:217:in `store_image!'
activesupport (3.2.6) lib/active_support/callbacks.rb:427:in `_run__2057521342514191385__save__4069718005166547559__callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.6) lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
activerecord (3.2.6) lib/active_record/callbacks.rb:264:in `create_or_update'
activerecord (3.2.6) lib/active_record/persistence.rb:84:in `save'
activerecord (3.2.6) lib/active_record/validations.rb:50:in `save'
activerecord (3.2.6) lib/active_record/attribute_methods/dirty.rb:22:in `save'
activerecord (3.2.6) lib/active_record/transactions.rb:241:in `block (2 levels) in save'
activerecord (3.2.6) lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
activerecord (3.2.6) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
activerecord (3.2.6) lib/active_record/transactions.rb:208:in `transaction'
newrelic_rpm (3.4.0.1) lib/new_relic/agent/method_tracer.rb:491:in `block in transaction_with_trace_ActiveRecord_self_name_transaction'
newrelic_rpm (3.4.0.1) lib/new_relic/agent/method_tracer.rb:242:in `trace_execution_scoped'
newrelic_rpm (3.4.0.1) lib/new_relic/agent/method_tracer.rb:486:in `transaction_with_trace_ActiveRecord_self_name_transaction'
activerecord (3.2.6) lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
activerecord (3.2.6) lib/active_record/transactions.rb:241:in `block in save'
activerecord (3.2.6) lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
activerecord (3.2.6) lib/active_record/transactions.rb:240:in `save'
app/controllers/pictures_controller.rb:60:in `create'
actionpack (3.2.6) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.6) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.6) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.6) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.6) lib/active_support/callbacks.rb:414:in `_run__3078682991788211307__process_action__1687541676864999030__callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.6) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.6) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.6) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.6) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.6) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.6) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.6) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.6) lib/action_controller/metal/params_wrapper.rb:206:in `process_action'
activerecord (3.2.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
newrelic_rpm (3.4.0.1) lib/new_relic/agent/instrumentation/rails3/action_controller.rb:34:in `block in process_action'
newrelic_rpm (3.4.0.1) lib/new_relic/agent/instrumentation/controller_instrumentation.rb:257:in `block in perform_action_with_newrelic_trace'
newrelic_rpm (3.4.0.1) lib/new_relic/agent/method_tracer.rb:242:in `trace_execution_scoped'
newrelic_rpm (3.4.0.1) lib/new_relic/agent/instrumentation/controller_instrumentation.rb:252:in `perform_action_with_newrelic_trace'
newrelic_rpm (3.4.0.1) lib/new_relic/agent/instrumentation/rails3/action_controller.rb:33:in `process_action'
actionpack (3.2.6) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.6) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.6) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.6) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.6) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:600:in `call'
newrelic_rpm (3.4.0.1) lib/new_relic/rack/browser_monitoring.rb:12:in `call'
newrelic_rpm (3.4.0.1) lib/new_relic/rack/developer_mode.rb:24:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:35:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/cookies.rb:338:in `call'
activerecord (3.2.6) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `_run__3679119317333771623__call__4069718005166547559__callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.6) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.6) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.6) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.6) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.6) lib/rails/engine.rb:479:in `call'
railties (3.2.6) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.6) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/Users/daniel.flynn/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Users/daniel.flynn/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Users/daniel.flynn/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
4

1 回答 1

0

在调用保存操作之前,我最终手动设置了这两个字段。这不是推荐的解决方案,但实际上这确实确保了这些参数中存在某些内容,并且在保存到数据库时不为零。

于 2012-07-16T20:58:25.933 回答