通过表单更改用户角色

时间:2009-11-19 21:17:00

标签: ruby-on-rails authentication roles acl9

我正在尝试创建一个Rails表单,允许管理员用户更改其他用户的指定角色。我创建的表单注册了对用户的更改(例如密码或登录更改),但没有注册对用户角色的更改,这是一个单独的模型。

我正在使用Acl9进行基于角色的身份验证,该身份验证使用用户模型,角色模型和链接这两者的R​​oles_user模型。

以下是我在表单中的代码:

<h1>Edit User</h1>

<% form_for(@user) do |form| %>
<%= error_messages_for :user %>
Login:                  <%= form.text_field :login %><br />
Password:               <%= form.password_field :password %><br />
Password Confirmation:  <%= form.password_field :password_confirmation %><br />

<% if current_user && current_user.has_role?(:admin) %>
    <%= error_messages_for :roles %>
    <% fields_for :roles do |role| %>
    <%= 
        @roles = Role.find(:all, :order => "name").map {|u| [u.name, u.id] }
        role.select(:id, @roles )
    %>
    <% end %>
   <% end %>
  <%= form.submit "Update" %>
<% end %>

<br />

<%= link_to "My Profile", account_path %>
<%= link_to "Logout", user_session_path, :method => :delete, :confirm => "Are you sure you want to logout?" %>

加载页面时我没有收到任何错误,但所选角色没有与用户关联。任何帮助,将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

您可能希望使用嵌套属性,这是Rails 2.3中的新增功能。以下链接可帮助您入门。