Rails 3到4错误的参数错误为空哈希参数

时间:2014-09-23 19:23:58

标签: ruby-on-rails ruby ruby-on-rails-4

在切换到强力参数后,我遇到了更新params的问题。我得到错误的参数错误,这是跟踪:

Started PATCH "/profiles/rails/about_me" for 127.0.0.1 at 2014-09-23 12:14:19 -0700
Processing by UsersController#about_me as 
  Parameters: {"utf8"=>"✓", "user"=>{"about_me"=>"testt"}, "commit"=>"Update Biography", "id"=>"rails"}
  User Load (0.5ms)  SELECT  "users".* FROM "users"  WHERE "users"."deleted" = 'f' AND "users"."id" = 10  ORDER BY "users"."id" ASC LIMIT 1
   (0.2ms)  SELECT COUNT(*) FROM "terms" INNER JOIN "terms_users" ON "terms"."id" = "terms_users"."term_id" WHERE "terms_users"."user_id" = $1 AND "terms"."name" = 'application'  [["user_id", 10]]
  User Load (0.5ms)  SELECT  "users".* FROM "users"  WHERE "users"."deleted" = 'f' AND "users"."slug" = 'rails'  ORDER BY "users"."id" ASC LIMIT 1
Completed 500 Internal Server Error in 14ms
127.0.0.1 POST /profiles/rails/about_me 500 Internal Server Error UsersController#about_me JS 13.8 (DB 0.0, View 0.0) {"utf8"=>"✓", "user"=>{"about_me"=>"testt"}, "commit"=>"Update Biography", "id"=>"rails"} {}
** [Airbrake] Notice was not sent due to configuration:         
  Environment Monitored? false         
  API key set? true

ArgumentError - wrong number of arguments (2 for 1):
  app/models/user.rb:347:in `update_profile'
  app/controllers/users_controller.rb:173:in `shared_update'
  app/controllers/users_controller.rb:72:in `about_me'
  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:149:in `block in halting_and_conditional'
  activesupport (4.1.6) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
  activesupport (4.1.6) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
  activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.6) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
  activesupport (4.1.6) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
  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 `block in halting'
  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 `block in halting'
  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 `block in halting'
  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'
  config/initializers/log_formatter.rb:22:in `block (2 levels) in <top (required)>'
  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'
  config/initializers/log_formatter.rb:21:in `block in <top (required)>'
  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'
  rack-mini-profiler (0.9.2) lib/mini_profiler/profiling_methods.rb:108:in `block in profile_method'
  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 `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 `call'
  actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:678:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  apipie-rails (0.2.6) lib/apipie/static_dispatcher.rb:65:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  omniauth (1.2.2) lib/omniauth/strategy.rb:186:in `call!'
  omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  bullet (4.13.2) lib/bullet/rack.rb:12:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  apipie-rails (0.2.6) lib/apipie/extractor/recorder.rb:97:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/rack/error_collector.rb:50:in `traced_call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/rack/agent_hooks.rb:26:in `traced_call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/rack/browser_monitoring.rb:23:in `traced_call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/rack/developer_mode.rb:48:in `traced_call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55: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 `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.5.2) lib/rack/etag.rb:23:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.5.2) lib/rack/head.rb:11:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  remotipart (1.2.1) lib/remotipart/middleware.rb:27:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/flash.rb:254:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57: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'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  activerecord (4.1.6) lib/active_record/query_cache.rb:36:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57: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'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  airbrake (4.1.0) lib/airbrake/rails/middleware.rb:13:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  better_errors (2.0.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (2.0.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (2.0.0) lib/better_errors/middleware.rb:57:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57: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'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  activesupport (4.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/static.rb:64:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  heroku-deflater (0.5.3) lib/heroku-deflater/skip_binary.rb:19:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.5.2) lib/rack/deflater.rb:25:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  heroku-deflater (0.5.3) lib/heroku-deflater/serve_zipped_assets.rb:50:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  airbrake (4.1.0) lib/airbrake/user_informer.rb:16:in `_call'
  airbrake (4.1.0) lib/airbrake/user_informer.rb:12:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack-mini-profiler (0.9.2) lib/mini_profiler/profiler.rb:300:in `call'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  railties (4.1.6) lib/rails/engine.rb:514:in `call'
  railties (4.1.6) lib/rails/application.rb:144:in `call'
  railties (4.1.6) lib/rails/railtie.rb:194:in `method_missing'
  newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  thin (1.6.2) lib/thin/connection.rb:86:in `block in pre_process'
  thin (1.6.2) lib/thin/connection.rb:84:in `pre_process'
  thin (1.6.2) lib/thin/connection.rb:53:in `process'
  thin (1.6.2) lib/thin/connection.rb:39:in `receive_data'
  eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'
  thin (1.6.2) lib/thin/backends/base.rb:73:in `start'
  thin (1.6.2) lib/thin/server.rb:162:in `start'
  rack (1.5.2) lib/rack/handler/thin.rb:16:in `run'
  rack (1.5.2) lib/rack/server.rb:264:in `start'
  railties (4.1.6) lib/rails/commands/server.rb:69:in `start'
  railties (4.1.6) lib/rails/commands/commands_tasks.rb:81:in `block in server'
  railties (4.1.6) lib/rails/commands/commands_tasks.rb:76:in `server'
  railties (4.1.6) lib/rails/commands/commands_tasks.rb:40:in `run_command!'
  railties (4.1.6) lib/rails/commands.rb:17:in `<top (required)>'
  script/rails:6:in `<main>'

User.rb:347:update_profile:

def update_profile(attributes, options = {})
  return false if attributes.nil?
  previous_avatar = !has_default_avatar?
  assign_attributes(attributes, options)
  if valid?
    if about_me_changed? || attributes[:avatar]
      uploaded_avatar = (attributes[:avatar].nil?)? false : true
      BadgesStrategies::Profile.assign_based_on(self, about_me: [about_me_was, about_me], avatar: [previous_avatar, uploaded_avatar])
    end
  end
  save
end

UsersController:shared_update:

def shared_update
  params[:user].delete(:password) if params[:user] && params[:user][:password].blank?
  params[:user].delete(:password_confirmation) if params[:user] && params[:user][:password].blank? and params[:user][:password_confirmation].blank?

  email_changed = params[:user] && params[:user][:email] && @user.email != params[:user][:email]
  if @user.update_profile(user_params)
    sign_in @user, bypass: true
    if email_changed
      if @user.pending_reconfirmation?
        flash[:notice] = "Success! Please check '#{params[:user][:email]}' for instructions to complete the update process."
      else
        flash[:notice] = "Successfully changed to '#{params[:user][:email]}'"
      end
    else
      flash[:notice] = "Your profile is successfully updated"
    end
  else
    flash[:alert] = (params[:user].nil? )? "ERROR: No user information sent for update" : "ERROR: #{@user.errors.full_messages.to_sentence}"
  end
end

UsersController:about_me:

def about_me
  shared_update
  render partial: 'users/about_me', locals: { user: @user }
end

有人可以看一看并告诉我到底出了什么问题吗?这是Rails 4中的语法错误吗?

1 个答案:

答案 0 :(得分:6)

如果user.rb第347行是此行:

assign_attributes(attributes, options)

然后就是你的问题。 ActiveRecord#assign_attributes似乎只在Rails 4中使用一个参数。 See docs here

Rails 3的方法API确实允许options哈希。 See docs here.

在rails 3中,options似乎已经处理了访问控制,允许配置谁能够进行批量分配。在具有强大参数的轨道4中,该责任已转移到参数哈希本身。

rails 4 docs说:

  

如果传递的哈希响应permitted?方法,并且此方法的返回值为false则会引发ActiveModel::ForbiddenAttributesError异常。


总之,当你收到一条消息告诉你你没有将正确的参数传递给方法时,你要做的第一件事就是在文档中查找该方法,看看你应该如何用它。

我不知道我什么时候读到这个问题的答案是什么。我刚刚找到了你将两个参数传递给你的代码的方法,然后查找了文档,答案非常明确。