我有一个使用嵌套模型的注册表单。客户可以拥有多个用户,User
属于Client
。在注册表单上,我希望能够同时注册客户端和用户。但是,我一直在为用户提供“无法大量分配”,因为我有accepts_nested_attributes_for :users
,所以我不明白。
<h1>Sign Up</h1>
<%= form_for @client do |client_form| %>
<% if @client.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% for message in @client.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= client_form.label :business_name %>
<%= client_form.text_field :business_name %>
</div>
<%= client_form.fields_for :user do |user_form| %>
<div class="field">
<%= user_form.label :first_name %>
<%= user_form.text_field :first_name %>
</div>
</div>
<div class="field">
<%= user_form.label :last_name %>
<%= user_form.text_field :last_name %>
</div>
<div class="field">
<%= user_form.label :username %>
<%= user_form.text_field :username %>
</div>
<div class="field">
<%= user_form.label :password %>
<%= user_form.password_field :password %>
</div>
<div class="field">
<%= user_form.label :password_confirmation %>
<%= user_form.password_field :password_confirmation %>
</div>
<% end %>
<div class="actions"><%= client_form.submit %></div>
<% end %>
class Client < ActiveRecord::Base
attr_accessible :business_name
attr_accessible :users_attributes
has_many :users
has_many :items
accepts_nested_attributes_for :users, allow_destroy: true
...
end
class User < ActiveRecord::Base
attr_accessible :client_id, :first_name, :last_name, :username, :password, :password_confirmation
belongs_to :client
has_secure_password
....
end
错误
ActiveModel::MassAssignmentSecurity::Error - Can't mass-assign protected attributes: user:
(gem) activemodel-3.2.11/lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'
(gem) activemodel-3.2.11/lib/active_model/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_removal'
(gem) activemodel-3.2.11/lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize'
(gem) activemodel-3.2.11/lib/active_model/mass_assignment_security.rb:230:in `sanitize_for_mass_assignment'
(gem) activerecord-3.2.11/lib/active_record/attribute_assignment.rb:75:in `assign_attributes'
(gem) activerecord-3.2.11/lib/active_record/base.rb:497:in `initialize'
app/controllers/clients_controller.rb:45:in `new'
app/controllers/clients_controller.rb:45:in `create'
(gem) actionpack-3.2.11/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
(gem) actionpack-3.2.11/lib/abstract_controller/base.rb:167:in `process_action'
(gem) actionpack-3.2.11/lib/action_controller/metal/rendering.rb:10:in `process_action'
(gem) actionpack-3.2.11/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
(gem) activesupport-3.2.11/lib/active_support/callbacks.rb:414:in `_run__330478543754893527__process_action__2034132490286778675
__callbacks'
这里有什么问题?
答案 0 :(得分:3)
更改
<%= client_form.fields_for :user do |user_form| %>
到
<%= client_form.fields_for :users do |user_form| %>
由于client_form引用了您的客户端模型和模型has_many :users
,因此您需要确保在使用嵌套表单字段时,使用:users
作为符号。另外,请确保您的客户端模型中有attr_accessible :users_attributes
。
答案 1 :(得分:1)
在您的客户端模型中尝试添加此方法:
def initialize(* args)
self.users.build
结束
OR
在你的控制器新动作中添加:
@user = @ client.users.build
答案 2 :(得分:-1)
只是一个猜测,但这有助于改变其中一条或两条线吗?
attr_accessible :users_attributes
accepts_nested_attributes_for :users, allow_destroy: true
到
attr_accessible :user_attributes
accepts_nested_attributes_for :user, allow_destroy: true