Admin :: MerchantsController中的ActiveModel :: ForbiddenAttributesError #create

时间:2016-05-24 12:14:53

标签: ruby-on-rails nested-forms fields-for

每次尝试保存下面的表单时,我都会收到ForbiddenAttrutesError

.mdl-grid.mdl-cell.mdl-cell--6-col.mdl-cell--4-offset
.mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
    = form.text_field :name, class: 'mdl-textfield__input'
    = form.label :name, class: 'mdl-textfield__label'
  .mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
    = form.email_field :email, class: 'mdl-textfield__input'
    = form.label :email, class: 'mdl-textfield__label'
  = form.fields_for :addresses, Address.new do |address_fields|
    .mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
      = address_fields.text_field :first_name, class: 'mdl-textfield__input'
      = address_fields.label :first_name, class: 'mdl-textfield__label'
    .mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
      = address_fields.text_field :last_name, class: 'mdl-textfield__input'
      = address_fields.label :last_name, class: 'mdl-textfield__label'
    .mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
      = address_fields.text_field :address1, class: 'mdl-textfield__input'
      = address_fields.label :address1, class: 'mdl-textfield__label'
    .mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
      = address_fields.text_field :address2, class: 'mdl-textfield__input'
      = address_fields.label :address2, class: 'mdl-textfield__label'
    .mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
      = address_fields.text_field :city, class: 'mdl-textfield__input'
      = address_fields.label :city, class: 'mdl-textfield__label'
    .mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
      = address_fields.text_field :zip_code, class: 'mdl-textfield__input'
      = address_fields.label :zip_code, class: 'mdl-textfield__label'

地址模型

class Address < ActiveRecord::Base

  belongs_to  :state
  belongs_to  :country
  belongs_to  :address_type
  belongs_to  :addressable, :polymorphic => true

商家模式

has_many    :addresses,       dependent: :destroy,       as: :addressable

  has_one     :default_billing_address,   -> { where(billing_default: true, active: true) },
              as:         :addressable,
              class_name: 'Address'

  has_many    :billing_addresses,         -> { where(active: true) },
              as:         :addressable,
              class_name: 'Address'

  has_one     :default_shipping_address,  -> { where(default: true, active: true) },
              as:         :addressable,
              class_name: 'Address'

  has_many     :shipping_addresses,       -> { where(active: true) },
               as:         :addressable,
               class_name: 'Address'

  before_validation :sanitize_data

  validates :name,        presence: true,       length: { maximum: 255 }
  validates :email,       format: { with: CustomValidators::Emails.email_validator },       :length => { :maximum => 255 }

  # geocoded_by :address
  # after_validation :geocode
  # after_create :sanitize_dates

  accepts_nested_attributes_for :addresses

商家控制器

private

  def allowed_params
    params.require(:merchant).permit(:name, :email, addresses_attributes: [:first_name, :last_name, :address1, :address2, :city, :zip_code, :country_id])
  end

完整错误消息

Started POST "/en/admin/merchants" for 127.0.0.1 at 2016-05-24 22:30:02 +1000
Processing by Admin::MerchantsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "merchant"=>{"name"=>"Test", "email"=>"test@test.com", "addresses_attributes"=>{"0"=>{"first_name"=>"Paul", "last_name"=>"M", "address1"=>"12 Dansu Ct", "address2"=>"", "city"=>"Hallam", "zip_code"=>"3803"}}}, "commit"=>"Create", "locale"=>"en"}
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."remember_token" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["remember_token", "5d0fec3c9ccb5afcd28845dd83eb18869f66b4b0"]]
Completed 500 Internal Server Error in 16ms (ActiveRecord: 0.4ms)

ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):
  activemodel (4.2.6) lib/active_model/forbidden_attributes_protection.rb:21:in `sanitize_for_mass_assignment'
  activerecord (4.2.6) lib/active_record/attribute_assignment.rb:33:in `assign_attributes'
  activerecord (4.2.6) lib/active_record/core.rb:566:in `init_attributes'
  activerecord (4.2.6) lib/active_record/core.rb:281:in `initialize'
  activerecord (4.2.6) lib/active_record/inheritance.rb:61:in `new'
  activerecord (4.2.6) lib/active_record/inheritance.rb:61:in `new'
  cancancan (1.14.0) lib/cancan/controller_resource.rb:80:in `build_resource'
  cancancan (1.14.0) lib/cancan/controller_resource.rb:61:in `load_resource_instance'
  cancancan (1.14.0) lib/cancan/controller_resource.rb:32:in `load_resource'
  cancancan (1.14.0) lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource'
  cancancan (1.14.0) lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
  activesupport (4.2.6) lib/active_support/callbacks.rb:448:in `instance_exec'
  activesupport (4.2.6) lib/active_support/callbacks.rb:448:in `block in make_lambda'
  activesupport (4.2.6) lib/active_support/callbacks.rb:164:in `block in halting'
  activesupport (4.2.6) lib/active_support/callbacks.rb:504:in `block in call'
  activesupport (4.2.6) lib/active_support/callbacks.rb:504:in `each'
  activesupport (4.2.6) lib/active_support/callbacks.rb:504:in `call'
  activesupport (4.2.6) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
  activesupport (4.2.6) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
  activesupport (4.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.2.6) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
  activesupport (4.2.6) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.6) lib/active_support/notifications.rb:164:in `instrument'
  actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.2.6) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.2.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.2.6) lib/abstract_controller/base.rb:137:in `process'
  actionview (4.2.6) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.2.6) lib/action_controller/metal.rb:196:in `dispatch'
  actionpack (4.2.6) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.2.6) lib/action_controller/metal.rb:237:in `block in action'
  actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
  actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:43:in `serve'
  actionpack (4.2.6) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.6) lib/action_dispatch/journey/router.rb:30:in `each'
  actionpack (4.2.6) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:817:in `call'
  bullet (5.1.0) lib/bullet/rack.rb:10:in `call'
  clearance (1.14.1) lib/clearance/rack_session.rb:23:in `call'
  rack (1.6.4) lib/rack/etag.rb:24:in `call'
  rack (1.6.4) lib/rack/conditionalget.rb:38:in `call'
  rack (1.6.4) lib/rack/head.rb:13:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/flash.rb:260:in `call'
  rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.2.6) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.6) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
  activesupport (4.2.6) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
  activesupport (4.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  web-console (3.1.1) lib/web_console/middleware.rb:131:in `call_app'
  web-console (3.1.1) lib/web_console/middleware.rb:28:in `block in call'
  web-console (3.1.1) lib/web_console/middleware.rb:18:in `catch'
  web-console (3.1.1) lib/web_console/middleware.rb:18:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.6) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.6) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.6) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.6) lib/rails/rack/logger.rb:20:in `call'
  quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
  actionpack (4.2.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/static.rb:120:in `call'
  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.6) lib/rails/engine.rb:518:in `call'
  railties (4.2.6) lib/rails/application.rb:165:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  puma (3.4.0) lib/puma/configuration.rb:224:in `call'
  puma (3.4.0) lib/puma/server.rb:569:in `handle_request'
  puma (3.4.0) lib/puma/server.rb:406:in `process_client'
  puma (3.4.0) lib/puma/server.rb:271:in `block in run'
  puma (3.4.0) lib/puma/thread_pool.rb:114:in `block in spawn_thread'

1 个答案:

答案 0 :(得分:0)

出现&#39; cancancan&#39; gem干扰了导致nested_forms问题的强参数。自从删除宝石以来,它解决了我的问题。