gpt4 book ai didi

ruby-on-rails - 无法批量分配 protected 属性 : user

转载 作者:行者123 更新时间:2023-12-04 05:07:45 25 4
gpt4 key购买 nike

我有一个使用嵌套模型的注册表单。客户端可以有多个用户,一个 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'

这里有什么问题?

最佳答案

改变
<%= 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在您的客户端模型中。

关于ruby-on-rails - 无法批量分配 protected 属性 : user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15304442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com