views:

133

answers:

1

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!

A: 

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

Colin Curtin
When I try to use nested attributes, I get a SQL exception: "no such column: users.user_id: SELECT * FROM "users" WHERE ("users".user_id = 12)." Changing the relevant line of code from "user_form.fields_for" to just "fields_for" takes care of the error, but leaves me back where I started, with the selected role not being associated with the user.
Eric K