0

我正在使用 Wicked gem 来帮助创建向导注册表单。当我尝试为我的 Camper 模型提交表单时,我一直收到质量分配错误,尽管我觉得我好像正确使用了 accepted_nested_attributes_for 和 attr_accessible 命令。为什么我会收到此错误?

以下是我的代码,任何进一步了解为什么会发生这种情况将不胜感激。

楷模

class Bill < ActiveRecord::Base
  has_one :parent
  has_many :campers
  accepts_nested_attributes_for :parent, :campers
  attr_accessible :email, :status, :comments, :campers_attributes
end

class Parent < ActiveRecord::Base
       belongs_to :bill
       attr_accessible   :addressone, :addresstwo, :cellnum, :city,
       :firstname, :heard, :homenum, :lastname, :referred, :state, :worknum, :zip
end

class Camper < ActiveRecord::Base
    belongs_to :bill
    attr_accessible :addressone, :addresstwo, :age, :city, :comments,
    :doctor, :emergencycontact, :firstname, :guardian, :health,
    :lastname, :medical, :state, :zip, :camper_attributes
end

表单的视图

<%= form_for current_bill, url: wizard_path do |f| %>
  <%= f.fields_for :camper do |c| %>
    <p>
      <%= c.label :firstname, "First name" %>
      <%= c.text_field :firstname %>
    </p>
    <p>
      <%= c.label :lastname, "Last name" %>
      <%= c.text_field :lastname %>
      </p>
      <p>
        <%= c.label :age, "Age" %>
        <%= c.text_field :age %>
      </p>
      <p>
        <%= c.label :addressone, "Address" %>
        <%= c.text_field :addressone %>
        <%= c.text_field :addresstwo %>
      </p>
      <p>
        <%= c.label :city, "City" %>
        <%= c.text_field :city %>
      </p>
      <p>
        <%= c.label :state, "State" %>
        <%= c.text_field :state %>
      </p>
      <p>
        <%= c.label :zip, "Zip code" %>
        <%= c.text_field :zip %>
      </p>
      <p>
        <%= c.label :emergencycontact, "Emergency contact (name, number)" %>
        <%= c.text_field :emergencycontact %>
      </p>
      <p>
        <%= c.label :guardian, "Guardian (name, number)" %>
        <%= c.text_field :guardian %>
      </p>
      <p>
        <%= c.label :doctor, "Doctor (name, number)"%>
        <%= c.text_field :doctor %>
      </p>
      <p>
        <%= c.label :health, "Health insurance (provider, plan number)" %>
        <%= c.text_field :health %>
      </p>
      <p>
        <%= c.label :medical, "Does this camper have any medical conditions?" %>
        <%= c.text_field :medical %>
      </p>
      <p>
        <%= c.label :comments, "Anything else we need to know?" %>
        <%= c.text_field :comments %>
      </p>
        <%= c.submit "Continue to Camp Selection" %>
<% end %>
<% end %>

 <%= form_for @bill, url: wizard_path do |f| %>
        <%= fields_for :parent do |p| %>
            <p>
        <%= p.label :firstname, "First name" %>
        <%= p.text_field :firstname %>
      </p>
      <p>
        <%= p.label :lastname, "Last name" %>
        <%= p.text_field :lastname %>
    </p>
    <p>
      <%= p.label :addressone, "Address" %>
      <%= p.text_field :addressone %>
      <%= p.text_field :addresstwo %>
    </p>
    <p>
      <%= p.label :city, "City" %>
      <%= p.text_field :city %>
    </p>
    <p>
      <%= p.label :state, "State" %>
      <%= p.text_field :state %>
    </p>
    <p>
      <%= p.label :zip, "Zip code" %>
      <%= p.text_field :zip %>
      </p>
    <p>
      <%= p.label :homenum, "Home number" %>
      <%= p.text_field :homenum %>
    </p>
    <p>
      <%= p.label :worknum, "Work number" %>
      <%= p.text_field :worknum %>
    </p>
    <p>
    <%= p.label :cellnum, "Cell number" %>
      <%= p.text_field :cellnum %>
    </p>
<p>
      <%= p.label :referred, "Did anyone refer you? If so, who?" %>
      <%= p.text_field :referred %>
    </p>
    <p>
    <%= p.label :heard, "How did you hear about us?" %>
      <%= p.text_area :heard, rows: 2 %>
      </p>
      <p class="pull-right">
      <%= p.submit "Continue" %>
      </p>
<% end %>
<% end %>

这是初始形式

    <%= form_for @bill do |f| %>
      <% if @bill.errors.any? %>
        <div class="error_messages">
          <h2><%= pluralize(@bill.errors.count, "error") %>
prohibited this bill from being saved:</h2>
          <ul>
            <% @bill.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
            <% end %>
          </ul>
        </div>
      <% end %>
    <div class="field">
     Email&nbsp;<%= f.text_field :email %>
    </div>
    <div class="actions pull-right">
      <%= f.submit "Continue" %>
    </div>
    <% end %>

控制器

class BillStepsController < ApplicationController
  include Wicked::Wizard
  steps :parent_registration, :camper_registration

  def show
    @bill = current_bill
    render_wizard
  end

  def update
    @bill = current_bill
    @bill.update_attributes(params[:bill])
    render_wizard @bill
  end

  def redirect_to_finish_wizard
          redirect_to root_url, notice: "Your registration is being processed"
  end
end

任何见识都非常感激。

编辑:这是我对错误的完整跟踪:

activemodel (3.2.8) lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'
activemodel (3.2.8) lib/active_model/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_removal'
activemodel (3.2.8) lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize'
activemodel (3.2.8) lib/active_model/mass_assignment_security.rb:230:in `sanitize_for_mass_assignment'
activerecord (3.2.8) lib/active_record/attribute_assignment.rb:75:in `assign_attributes'
activerecord (3.2.8) lib/active_record/persistence.rb:215:in `block in update_attributes'
activerecord (3.2.8) lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
activerecord (3.2.8) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
activerecord (3.2.8) lib/active_record/transactions.rb:208:in `transaction'
activerecord (3.2.8) lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
activerecord (3.2.8) lib/active_record/persistence.rb:214:in `update_attributes'
app/controllers/bill_steps_controller.rb:12:in `update'
actionpack (3.2.8) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.8) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.8) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.8) lib/active_support/callbacks.rb:436:in `_run__3054789496853050355__process_action__2475602958695063562__callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.8) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.8) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.8) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.8) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.8) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.8) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.8) 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.8) lib/action_dispatch/routing/route_set.rb:600:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:35:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/cookies.rb:339:in `call'
activerecord (3.2.8) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `_run__3006905520456248782__call__1949660601409229519__callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.8) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.8) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.8) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.8) lib/rails/engine.rb:479:in `call'
railties (3.2.8) lib/rails/application.rb:223:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.8) lib/rails/rack/log_tailer.rb:17:in `call'
thin (1.5.0) lib/thin/connection.rb:81:in `block in pre_process'
thin (1.5.0) lib/thin/connection.rb:79:in `catch'
thin (1.5.0) lib/thin/connection.rb:79:in `pre_process'
thin (1.5.0) lib/thin/connection.rb:54:in `process'
thin (1.5.0) lib/thin/connection.rb:39:in `receive_data'
eventmachine (1.0.0) lib/eventmachine.rb:187:in `run_machine'
eventmachine (1.0.0) lib/eventmachine.rb:187:in `run'
thin (1.5.0) lib/thin/backends/base.rb:63:in `start'
thin (1.5.0) lib/thin/server.rb:159:in `start'
rack (1.4.5) lib/rack/handler/thin.rb:13:in `run'
rack (1.4.5) lib/rack/server.rb:268:in `start'
railties (3.2.8) lib/rails/commands/server.rb:70:in `start'
railties (3.2.8) lib/rails/commands.rb:55:in `block in <top (required)>'
railties (3.2.8) lib/rails/commands.rb:50:in `tap'
railties (3.2.8) lib/rails/commands.rb:50:in `<top (required)>'
script/rails:6:in `require'
script/rails:6:in `<main>'

和简单的错误响应:

ActiveModel::MassAssignmentSecurity::Error in BillStepsController#update

Can't mass-assign protected attributes: camper
4

2 回答 2

0

在您的 Bill 课程中,尝试以下更改:

attr_accessible :email, :status, :comments, :campers_attributes

attr_accessible :email, :status, :comments, :campers, :campers_attributes

我添加了 :campers

于 2013-05-25T17:07:27.890 回答
0

根据 Rails api 文档:

Accepts_nested_attributes_for:为指定的关联定义一个属性编写器。如果您使用 attr_protected 或 attr_accessible,则需要将属性 writer 添加到允许列表中。

所以,我认为你必须添加campers到你的attr_accesiblein Bill 模型中。

希望这有帮助!

于 2013-05-25T17:08:24.157 回答