代码之家  ›  专栏  ›  技术社区  ›  minohimself

Rails强参数ActiveModel::ForbidenAttributesError

  •  -1
  • minohimself  · 技术社区  · 9 年前

    嗨,我知道Rails4使用了强参数,我需要在控制器中允许它们,这就是我所做的,但我仍然会收到这个错误。

    我的请求看起来像

    {"utf8"=>"✓",
    "authenticity_token"=>"cAJLJGiywr4TeAEpidQ1/XstiSNO+819W4QBhgMGNczVret+H/XDDVk3l1UpoaK8tHVK6qf+KK8VjBdM51zocA==",
    "estate"=>{"building_id"=>"1",
    "planet_id"=>"1",
    "number"=>"2"},
    "commit"=>"Build",
    "locale"=>"en"}
    

    在我的控制器中,我尝试了

    def estate_params
        params.require(:estate).permit(:building_id, :planet_id, :number)
    end
    
    
    @estate = Estate.new(estate_params)
    

    Estate.new(params[:estate].permit(:building_id, :planet_id, :number))
    

    有人能告诉我为什么会出现这个错误吗?

    谢谢

    完整跟踪

    .bundle/gems/activemodel-4.2.0/lib/active_model/forbidden_attributes_protection.rb:21:in     `sanitize_for_mass_assignment'
    .bundle/gems/activerecord-4.2.0/lib/active_record/attribute_assignment.rb:33:in `assign_attributes'
    .bundle/gems/activerecord-4.2.0/lib/active_record/core.rb:557:in `init_attributes'
    .bundle/gems/activerecord-4.2.0/lib/active_record/core.rb:280:in `initialize'
    .bundle/gems/activerecord-4.2.0/lib/active_record/inheritance.rb:61:in `new'
    .bundle/gems/activerecord-4.2.0/lib/active_record/inheritance.rb:61:in `new'
    .bundle/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:85:in `build_resource'
    .bundle/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:66:in `load_resource_instance'
    .bundle/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:32:in `load_resource'
    .bundle/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource'
    .bundle/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
    .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:443:in `instance_exec'
    .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:443:in `block in make_lambda'
    .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:163:in `call'
    .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:163:in `block in halting'
    .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:169:in `call'
    .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:169:in `block in halting'
    .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:151:in `call'
    .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
    .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:234:in `call'
    .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:234:in `block in halting'
    .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:234:in `call'
    .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:234:in `block in halting'
    .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:169:in `call'
    .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:169:in `block in halting'
    .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:169:in `call'
    .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:169:in `block in halting'
    .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:169:in `call'
    

    .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:169:in block in halting' .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in 呼叫' .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in _run_callbacks' .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:734:in _run_process_action_callbacks' .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:81:in run_callbacks' .bundle/gems/actionpack-4.2.0/lib/abstract_controller/callbacks.rb:19:in 进程操作' .bundle/gems/actionpack-4.2.0/lib/action_controller/metal/erescue.rb:29:in process_action' .bundle/gems/actionpack-4.2.0/lib/action_controller/metal/instrumentation.rb:31:in process_action中的块' .bundle/gems/activesupport-4.2.0/lib/active_support/notifications.rb:164:in block in instrument' .bundle/gems/activesupport-4.2.0/lib/active_support/notifications/instrumenter.rb:20:in 仪器' .bundle/gems/activesupport-4.2.0/lib/active_support/notifications.rb:164:in instrument' .bundle/gems/actionpack-4.2.0/lib/action_controller/metal/instrumentation.rb:30:in 进程操作' .bundle/gems/actionpack-4.2.0/lib/action_controller/metal/params_wrapper.rb:250:in process_action' .bundle/gems/activerecord-4.2.0/lib/active_record/railties/controller_runtime.rb:18:in 进程操作' .bundle/gems/actionpack-4.2.0/lib/abstract_controller/base.rb:137:in process' .bundle/gems/actionview-4.2.0/lib/action_view/rendering.rb:30:in 进程' .bundle/gems/actionpack-4.2.0/lib/action_controller/metal.rb:195:in dispatch' .bundle/gems/actionpack-4.2.0/lib/action_controller/metal/rack_delegation.rb:13:in 调度' .bundle/gems/actionpack-4.2.0/lib/action_controller/metal.rb:236:in block in action' .bundle/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:73:in 呼叫' .bundle/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:73:in dispatch' .bundle/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:42:in 服务' .bundle/gems/actionpack-4.2.0/lib/action_dispatch/reporter/router.rb:43:in block in serve' .bundle/gems/actionpack-4.2.0/lib/action_dispatch/journey/router.rb:30:in 每个' .bundle/gems/actionpack-4.2.0/lib/action_dispatch/reporter/router.rb:30:in serve' .bundle/gems/actionpack-4.2.0/lib/action_dispatch/routing/route_set.rb:802:in 呼叫' .bundle/ggems/rack-pjax-0.8.0/lib/rack/pjax.rb:12:in call' .bundle/gems/warden-1.2.3/lib/warden/manager.rb:35:in 调用中的块' .bundle/ggems/wardn-1.2.3/lib/warder/manager.rb:34:in catch' .bundle/gems/warden-1.2.3/lib/warden/manager.rb:34:in 呼叫' .breel/gems/rack-1.6.4/lib/rack/etag.rb:24:in call' .bundle/gems/rack-1.6.4/lib/rack/conditionalget.rb:38:in 呼叫' .breer/gems/rack-1.6.4/lib/rack/head.rb:13:in call' .bundle/gems/remotipart-1.2.1/lib/remotipart/middleware.rb:27:in 呼叫' .bundle/gems/actionpack-4.2.0/lib/action_dispatch/middleware/params_parser.rb:27:in call' .bundle/gems/actionpack-4.2.0/lib/action_dispatch/middleware/flash.rb:260:in 呼叫' .bundle/gems/rrack-1.6.4/lib/rack/session/abstract/id.rb:225:in context' .bundle/gems/rack-1.6.4/lib/rack/session/abstract/id.rb:220:in 呼叫' .bundle/gems/actionpack-4.2.0/lib/action_dispatch/middleware/cookies.rb:560:in call' .bundle/gems/activerecord-4.2.0/lib/active_record/query_cache.rb:36:in 呼叫' .bundle/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in call' .bundle/gems/activerecord-4.2.0/lib/active_record/migration.rb:378:in 呼叫' .bundle/gems/actionpack-4.2.0/lib/action_dispatch/middleware/callbacks.rb:29:in block in call' .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:88:in 呼叫' .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:88:in _运行_回调' .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:734:in _运行_调用_回调' .bundle/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:81:in run_callbacks' .bundle/gems/actionpack-4.2.0/lib/action_dispatch/middleware/callbacks.rb:27:in 呼叫' .bundle/gems/actionpack-4.2.0/lib/action_dispatch/middleware/reloader.rb:73:in call' .bundle/gems/actionpack-4.2.0/lib/action_dispatch/middleware/remote_ip.rb:78:in 呼叫' .bundle/gems/better_errors-2.1.1/lib/better_errors/middleware.rb:59:in call' .bundle/gems/actionpack-4.2.0/lib/action_dispatch/middleware/debug_exceptions.rb:17:in 呼叫' .bundle/gems/web-console--2.2.1/lib/web_console/middleware.rb:31:in call' .bundle/gems/actionpack-4.2.0/lib/action_dispatch/middleware/show_exceptions.rb:30:in 呼叫' .breel/gems/railties-4.2.0/lib/rails/rack/logger.rb:38:in call_app' .bundle/gems/railties-4.2.0/lib/rails/rack/logger.rb:20:in 调用中的块' .bundle/gems/activesupport-4.2.0/lib/active_support/tagged_logging.rb:68:in block in tagged' .bundle/gems/activesupport-4.2.0/lib/active_support/tagged_logging.rb:26:in 已标记' .bundle/gems/activesupport-4.2.0/lib/active_support/tagged_logging.rb:68:in tagged' .bundle/gems/railties-4.2.0/lib/rails/rack/logger.rb:20:in 呼叫' .bundle/gems/quiet_assets-1.0/lib/quiet_assets.rb:27:in call_with_quiet_assets' .bundle/gems/actionpack-4.2.0/lib/action_dispatch/middleware/request_id.rb:21:in 呼叫' .bundle/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in call' .bundle/gems/rack-1.6.4/lib/rack/runtime.rb:18:in 呼叫' .bundle/gems/activesupport-4.2.0/lib/active_support/cache/storty/local_cache_middleware.rb:28:in call' .bundle/gems/actionpack-4.2.0/lib/action_dispatch/middleware/static.rb:113:in 呼叫' .bundle/gems/rack-1.6.4/lib/rack/sendfile.rb:113:in call' .bundle/gems/railties-4.2.0/lib/rails/engine.rb:518:in 呼叫' .breel/gems/railties-4.2.0/lib/rails/application.rb:164:in call' .bundle/gems/rack-1.6.4/lib/rack/content_length.rb:15:in 呼叫' .bundle/ggems/thin-1.6.3/lib/thin/connection.rb:86:in block in pre_process' .bundle/gems/thin-1.6.3/lib/thin/connection.rb:84:in 捕获' .bundle/ggems/thin-1.6.3/lib/thin/connection.rb:84:in pre_process' .bundle/gems/thin-1.6.3/lib/thin/connection.rb:53:in 进程' .breel/gems/faye-websocket-0.10.0/lib/faye/adapters/thin.rb:40:in process' .bundle/gems/thin-1.6.3/lib/thin/connection.rb:39:in 接收数据' .breel/gems/faye-websocket-0.10.0/lib/faye/adapters/thin.rb:44:in receive_data' .bundle/gems/eventmachine-1.0.8/lib/eventmachine.rb:193:in 运行_机器' .bundle/gems/eventmachine1.0.8/lib/eventmachine.rb:193:in run' .bundle/gems/thin-1.6.3/lib/thin/backends/base.rb:73:in 开始' .bundle/ggems/thin-1.6.3/lib/thin/server.rb:162:in start' .bundle/gems/rack-1.6.4/lib/rack/handler/thin.rb:19:in 运行' .bundle/gems/rack-1.6.4/lib/rack/server.rb:286:in start' .bundle/gems/railties-4.2.0/lib/rails/commands/server.rb:80:in 开始' .bundle/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:80:in block in server' .bundle/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:75:in 点击' .bundle/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:75:in server' .bundle/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in run_command!' .bundle/gems/railties-4.2.0/lib/rails/commands.rb:17:in <top (required)>' bin/rails:8:in 要求' 箱柜/导轨:8:in <top (required)>' .bundle/gems/spring-1.3.6/lib/spring/client/rails.rb:28:in 负载' .bundle/gems/spring-1.3.6/lib/spring/client/rails.rb:28:in call' .bundle/gems/spring-1.3.6/lib/spring/client/command.rb:7:in 呼叫' .bundle/gems/spring-1.3.6/lib/spring/client.rb:26:in run' .bundle/gems/spring-1.3.6/bin/spring:48:in ' .bundle/gems/spring-1.3.6/lib/spring/binstub.rb:11:in load' .bundle/gems/spring-1.3.6/lib/spring/binstub.rb:11:in ' 箱柜/弹簧:13:in require' bin/spring:13:in ' 箱柜/导轨:3:英寸 load' bin/rails:3:in '

    1 回复  |  直到 9 年前
        1
  •  1
  •   Zepplock    9 年前

    您需要将其添加到控制器的顶部(假设这是一个POST Estate )

    load_and_authorize_resource :except => [:create]