开发者

updating user record with devise that has_one relationship

Im using devise to handle my user authentication and in my user model Ive stated that each user has_one :role.

Im using another table to hold all my user roles/permissions and I was wondering how to update the role?

EDIT - here is my user model

  has_one :role, :de开发者_StackOverflow社区pendent => :destroy
  accepts_nested_attributes_for :role, :allow_destroy => true
  attr_accessible :stuff.... :role

My role model

 belongs_to :user

Ive added this to my form:

<%= f.fields_for :role, Role.new do |r| %>
<li class="full_width">
  <%= r.label "User type" %>
  <%= r.select(:status, %w[member artist commercial],{:include_blank => false}) %>
</li>
<% end %>

but it never saves the role record, I guess its because the user model didnt have attr_accessible :role so I set that up and now when I try to save I get a AssociationTypeMismatch error

EDIT - added the accepts_attributes_for and now I dont get the error but the role record isnt saved. Console shows

WARNING: Can't mass-assign protected attributes: role_attributes


See http://api.rubyonrails.org/classes/ActiveModel/MassAssignmentSecurity/ClassMethods.html#method-i-attr_accessible. You have to declare

attr_accessible :role_attributes


From the code snippet that you have pasted, it's not clear where you are building the association between the new Role and the User. You may need to do something like @user.build_role(...) or @role.build_user(...) in order to associate the user and the role prior to saving.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜