1

「accepts_nested_attributes_for:people」を機能させるのに問題があります。目標は、ユーザー(私のアプリでは「Person」と呼ばれます)がサインアップ時に家族の名前を確立できるようにすることです。家族は人とは別のテーブルにいます。私はこれに関する多くのスタックオーバーフローの投稿を読みましたが、どのソリューションも機能していないようです。このエラーで繰り返し発生するテーマは、attr_accessibleに:people_attributesを追加するのを忘れています。私はこれを正しく行ったと思います。コントローラの何かを忘れている可能性があります。

私は家族対人と1対多の関係を持っています

これが私の関連コードです:

家族モデル:

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

  has_many :adventures
  has_many :people

  accepts_nested_attributes_for :people

end

人物モデル

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

ファミリービュー

<%= 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 %>

以下は、開発ログからの完全なトレースです

    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'
4

1 に答える 1

3

の別のテーマは、accepts_nested_attributes_forこれを書いている人々です:

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

それ以外の

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

を渡す:personfields_for、レールがを探しperson_attributesます。

そして、コントローラーに人を作成する必要があります@family.people.build。フォームを表示するために新しい@familyレコードを作成するのと同じ方法で、ネストされたセクションを表示するために家族に属する個人レコードを作成する必要があります。

于 2013-03-09T22:29:59.907 に答える