简体   繁体   English

通过表单更改用户角色

[英]Changing user roles through a form

I'm trying to create a Rails form that allows an admin user to change the assigned roles of other users. 我正在尝试创建一个允许管理员用户更改其他用户的分配角色的Rails表单。 The form I've created registers changes to the user (such as password or login changes), but doesn't register changes to the user's role, which is a separate model. 我创建的表单会向用户注册更改(例如密码或登录更改),但不会注册对用户角色的更改,这是一个单独的模型。

I'm using Acl9 for role-based authentication, which uses a User model, a Role model, and a Roles_user model which links the two. 我正在使用Acl9进行基于角色的身份验证,该身份验证使用用户模型,角色模型和链接两者的Roles_user模型。

Here's the code I have in my form: 这是我的表单中的代码:

<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?" %>

I don't get any errors when loading the page, but the selected role doesn't get associated to the user. 加载页面时没有出现任何错误,但是所选角色没有与用户相关联。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

You probably want to use nested attributes, which is new in Rails 2.3. 您可能想使用嵌套属性,这是Rails 2.3中的新增功能。 The following links should help get you started. 以下链接应帮助您入门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM