Ruby on rails 使用具有多个角色的设计用户的表单的字段_中的批量分配错误

Ruby on rails 使用具有多个角色的设计用户的表单的字段_中的批量分配错误,ruby-on-rails,forms,nested-forms,mass-assignment,Ruby On Rails,Forms,Nested Forms,Mass Assignment,我研究了很多与嵌套属性中的质量分配相关的问题,但没有一个能帮我解决 我正在使用Desive,并拥有一个不同类型的用户模型,如客户端模型: class User < ActiveRecord::Base attr_accessible :email, :name, :password, :password_confirmation, :remember_me, :rolable_type belongs_to :rolable, :polymorphic => true a

我研究了很多与嵌套属性中的质量分配相关的问题,但没有一个能帮我解决

我正在使用Desive,并拥有一个不同类型的
用户
模型,如
客户端
模型:

class User < ActiveRecord::Base
  attr_accessible :email, :name, :password, :password_confirmation, :remember_me, :rolable_type
  belongs_to :rolable, :polymorphic => true
  accepts_nested_attributes_for :rolable
end

class Client < ActiveRecord::Base
  has_one :user, :as => :rolable
  attr_accessible :specialty, :address
end
这是我的
userscoontroller
更新操作:

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      flash[:success] = "Profile updated"
      sign_in @user
      redirect_to @user
    else
      render 'edit'
    end
  end

您需要在用户类中为:rolable创建一个accepts\u嵌套的\u attributes\u

请参阅:

OP提供的解决方案

我为
更改了


它起作用了

我现在遇到了一个批量分配错误,而我在
用户
类中添加了
接受嵌套的属性\u:rolable
无法批量分配受保护的属性:client
(感谢您的帮助!)尝试将其更改为接受:client的嵌套属性,我认为您需要将:client添加到属性可访问列表最奇怪的是,即使在设置
config.active\u record.whitelist\u attributes=false
时,我仍然会遇到
mass assignment错误。。。。
    <%= form_for(@user) do |f| %>
       <%= render 'shared/error_messages', object: f.object %>

[...]

      <% f.fields_for(@user.rolable) do |rf| -%>

        <div><%= rf.label :specialty %><br />
        <%= rf.text_field :specialty %></div>

        <div><%= rf.label :address %><br />
        <%= rf.text_field :address %></div>
      <% end %>

[...]
  def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      flash[:success] = "Profile updated"
      sign_in @user
      redirect_to @user
    else
      render 'edit'
    end
  end