lando2319
lando2319

Reputation: 1810

Mass-Assignment Error with Nested Attributes

I am having trouble getting the "accepts_nested_attributes_for :people" to work. The goal is for the user (called "Person" in my app) to be able to establish a family name upon signup. Family is on a separate table from person. I've read through lots of stack overflow posts on this but none of the solutions seem to be working. The reoccurring theme with this error is forgetting to add :people_attributes to the attr_accessible. I believe I have done this correctly. I may be forgetting something in the controller.

I have a one-to-many relationship with Family to Person

Here is my relevant code:

Family Model:

class Family < ActiveRecord::Base
  attr_accessible :name, :people_attributes

  has_many :adventures
  has_many :people

  accepts_nested_attributes_for :people

end

Person Model

class Person < ActiveRecord::Base
  attr_accessible :age, :first_name, :last_name, :nickname, :family_id, :password, :password_confirmation

  belongs_to :family
  has_many :reviews


  has_secure_password
end

Family View

<%= form_for(@family) do |f| %>
 <% if @family.errors.any? %>
   <div id="error_explanation">
     <h2><%= pluralize(@family.errors.count, "error") %> prohibited this family from being saved:</h2>

    <ul>
     <% @family.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

<table>
 <tr>
      <td class="form-cell"><%= f.label :name %></td>
      <td class="form-cell"><%= f.text_field :name %></td>
 </tr>
  <tr>
      <td class="form-cell"><%= f.fields_for :person do |ts| %></td>
        <td class="form-cell"><%= ts.label :first_name %></td>
        <td class="form-cell"><%= ts.text_field :first_name %></td>
        </tr>
      <tr>
         <td class="form-cell"><%= ts.label :last_name %></td>
        <td class="form-cell"><%= ts.text_field :last_name %></td>
      </tr>
      <tr>
        <td class="form-cell"><%= ts.label :age %></td>
        <td class="form-cell"><%= ts.text_field :age %></td>
      </tr>
      <tr>
        <td class="form-cell"><%= ts.label :nickname %></td>
        <td class="form-cell"><%= ts.text_field :nickname %></td>
      </tr>
      <tr>
       <td class="form-cell"><%= ts.label :password %></td>
       <td class="form-cell"><%= ts.password_field :password %></td>
    </tr>
    <tr>
       <td class="form-cell"><%= ts.label :password_confirmation %></td>
       <td class="form-cell"><%= ts.password_field :password_confirmation %></td>
    </tr>
<% end %>
 <%= f.submit %>

Below is my Full Trace from my Development log

    activemodel (3.2.12) lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'
    activemodel (3.2.12) lib/active_model/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_removal'
    activemodel (3.2.12) lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize'
    activemodel (3.2.12) lib/active_model/mass_assignment_security.rb:230:in `sanitize_for_mass_assignment'
    activerecord (3.2.12) lib/active_record/attribute_assignment.rb:75:in `assign_attributes'
    activerecord (3.2.12) lib/active_record/base.rb:497:in `initialize'
    app/controllers/families_controller.rb:43:in `new'
    app/controllers/families_controller.rb:43:in `create'
    actionpack (3.2.12) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
    actionpack (3.2.12) lib/abstract_controller/base.rb:167:in `process_action'
    actionpack (3.2.12) lib/action_controller/metal/rendering.rb:10:in `process_action'
    actionpack (3.2.12) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
    activesupport (3.2.12) lib/active_support/callbacks.rb:414:in `_run__4160186070491300860__process_action__2492885545127699891__callbacks'
    activesupport (3.2.12) lib/active_support/callbacks.rb:405:in `__run_callback'
    activesupport (3.2.12) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
    activesupport (3.2.12) lib/active_support/callbacks.rb:81:in `run_callbacks'
    actionpack (3.2.12) lib/abstract_controller/callbacks.rb:17:in `process_action'
    actionpack (3.2.12) lib/action_controller/metal/rescue.rb:29:in `process_action'
    actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
    activesupport (3.2.12) lib/active_support/notifications.rb:123:in `block in instrument'
    activesupport (3.2.12) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    activesupport (3.2.12) lib/active_support/notifications.rb:123:in `instrument'
    actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
    actionpack (3.2.12) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
    activerecord (3.2.12) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
    actionpack (3.2.12) lib/abstract_controller/base.rb:121:in `process'
    actionpack (3.2.12) lib/abstract_controller/rendering.rb:45:in `process'
    actionpack (3.2.12) lib/action_controller/metal.rb:203:in `dispatch'
    actionpack (3.2.12) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
    actionpack (3.2.12) lib/action_controller/metal.rb:246:in `block in action'
    actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:73:in `call'
    actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
    actionpack (3.2.12) 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.12) lib/action_dispatch/routing/route_set.rb:601:in `call'
    actionpack (3.2.12) 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.12) lib/action_dispatch/middleware/head.rb:14:in `call'
    actionpack (3.2.12) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
    actionpack (3.2.12) 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.12) lib/action_dispatch/middleware/cookies.rb:341:in `call'
    activerecord (3.2.12) lib/active_record/query_cache.rb:64:in `call'
    activerecord (3.2.12) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
    actionpack (3.2.12) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
    activesupport (3.2.12) lib/active_support/callbacks.rb:405:in `_run__555282021550517493__call__927558071375684288__callbacks'
    activesupport (3.2.12) lib/active_support/callbacks.rb:405:in `__run_callback'
    activesupport (3.2.12) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
    activesupport (3.2.12) lib/active_support/callbacks.rb:81:in `run_callbacks'
    actionpack (3.2.12) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
    actionpack (3.2.12) lib/action_dispatch/middleware/reloader.rb:65:in `call'
    actionpack (3.2.12) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
    actionpack (3.2.12) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
    actionpack (3.2.12) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
    railties (3.2.12) lib/rails/rack/logger.rb:32:in `call_app'
    railties (3.2.12) lib/rails/rack/logger.rb:16:in `block in call'
    activesupport (3.2.12) lib/active_support/tagged_logging.rb:22:in `tagged'
    railties (3.2.12) lib/rails/rack/logger.rb:16:in `call'
    actionpack (3.2.12) 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.12) 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.12) lib/action_dispatch/middleware/static.rb:62:in `call'
    railties (3.2.12) lib/rails/engine.rb:479:in `call'
    railties (3.2.12) lib/rails/application.rb:223:in `call'
    rack (1.4.5) lib/rack/content_length.rb:14:in `call'
    railties (3.2.12) lib/rails/rack/log_tailer.rb:17:in `call'
    rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
    /Users/mikeland/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
    /Users/mikeland/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
    /Users/mikeland/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

Upvotes: 1

Views: 228

Answers (1)

Robin
Robin

Reputation: 21884

Another theme with accepts_nested_attributes_for is people writing this:

<%= f.fields_for :person do |ts| %>

instead of

<%= f.fields_for :people do |ts| %>

If you pass :person in fields_for, then rails is going to be looking for person_attributes.

And you'll need to build a person in the controller: @family.people.build. The same way you build a new @family record to display the form, you need to build a person record belonging to the family to display the nested section.

Upvotes: 3

Related Questions