通过表单更改用户角色
我正在尝试创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想要使用嵌套属性,这是 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.