通过表单更改用户角色

发布于 2024-08-11 15:03:10 字数 1176 浏览 6 评论 0原文

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

我使用 Acl9 进行基于角色的身份验证,它使用用户模型、角色模型和链接两者的 Roles_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?" %>

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

I'm trying to create a Rails form that allows an admin user to change the assigned roles of other users. 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.

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一杯敬自由 2024-08-18 15:03:10

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

You probably want to use nested attributes, which is new in Rails 2.3. The following links should help get you started.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文