I looked on plenty of issues related to mass-assignement in nested attributes but none of them helped me out...
I am using Devise and have one User
model with different types, as the Client
one:
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
I have the following form for creating a new user with the client type and client attributes and which is working:
[...]
resource.rolable = child_class_name.constantize.new if resource.rolable.nil?
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
[...]
<% # customized code begin %>
<%= fields_for resource.rolable do |rf| %>
<% if rolable_type == 'client' %>
<div><%= rf.label :specialty %><br />
<%= rf.text_field :specialty %></div>
[...]
<% end %>
<% end %>
<%= hidden_field :user, :rolable_type, :value => rolable_type %>
<% # customized code end %>
[...]
<div><%= f.submit "Sign up" %></div>
<% end %>
I have the following for editing the same user and which is not working: I am getting the following error : ActiveModel::MassAssignmentSecurity::Error in UsersController#update
: Can't mass-assign protected attributes: client
<%= 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 %>
[...]
Here is my UsersController
update action:
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