我无法收到“accepts_nested_attributes_for:people” 上班。目标是用户(在我的应用程序中称为“人”)能够在注册时建立姓氏。家人与人分开。我已经阅读了很多堆栈溢出帖子,但没有一个解决方案似乎有效。出现此错误的重复主题是忘记将:people_attributes添加到attr_accessible。我相信我已经做到了这一点。我可能会忘记控制器中的某些东西。
我与家人对人有一对多的关系
以下是我的相关代码:
家庭模式:
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'
答案 0 :(得分:3)
accepts_nested_attributes_for
的另一个主题是人们写这个:
<%= f.fields_for :person do |ts| %>
而不是
<%= f.fields_for :people do |ts| %>
如果您在:person
中传递fields_for
,那么rails将会寻找person_attributes
。
你需要在控制器中建立一个人:@family.people.build
。与构建新的@family记录以显示表单的方式相同,您需要构建属于该族的人员记录以显示嵌套部分。