Assign and edit roles through application

6 views
Skip to first unread message

Eric Kurzenberger

unread,
Nov 17, 2009, 11:43:26 AM11/17/09
to acl9-discuss
Hello,

I'm developing a Rails app (my first!) which will have multiple users
and roles (admin, staff, etc.). Users will either be anonymous, or
have one specific global role, with only the admin user having the
ability to create or delete users. I would like to have an admin user
be able to assign roles to other users when he creates them, or change
the roles for existing users. Is this doable? I know users can be
assigned roles using "@user.has_role!(:admin, object = nil )" in the
controller, but I don't know how to do this dynamically through the
application.

Any help would be appreciated!

Cheers,

Eric

Dotan N.

unread,
Nov 18, 2009, 4:36:02 AM11/18/09
to acl9-d...@googlegroups.com
i've implemented this kind of functionality. using what you gave as an example, what is your difficulty exactly?

Eric Kurzenberger

unread,
Nov 18, 2009, 9:51:02 AM11/18/09
to acl9-discuss
I'm having difficulty coming up with the code for the Users controller
and view that would enable the admin user to set or change the role of
another user. I currently have the Users index page set up to list
users and their roles like this:

<% for user in @users %>
<tr>
<td><%=h user.login %></td>
<td><%=h user.roles_for(nil).map(&:name) %> </td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_account_path %></td>
<td><%= link_to 'Destroy', user, :confirm => 'Are you
sure?', :method => :delete %></td>
</tr>
<% end %>

What I want to do is change the field that lists the user's role to a
dropdown listing the roles that can be changed. But I've no idea how
to code it.

Thanks for the help.

Eric

Dotan N.

unread,
Nov 18, 2009, 10:57:52 AM11/18/09
to acl9-d...@googlegroups.com
have you considered exposing an action in the controller that sets or removes roles from user?
then you can call it with specified user id and role and action.

Eric Kurzenberger

unread,
Nov 18, 2009, 12:36:45 PM11/18/09
to acl9-discuss
What I was planning on doing was having an area of the edit_user form
available only to admins that would allow the admin to select a role,
with the list of names of available roles pulled from the Role table.
Something like this:

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

But that returns an error: "undefined method `name' for #<User:
0x104a640f0>." I'm thinking it's because I'm trying to pull the role
information from a different model (the Role model), but I'm not sure
how to fix it.

Eric

Jason King

unread,
Nov 18, 2009, 1:30:29 PM11/18/09
to acl9-d...@googlegroups.com
Eric,

There are not Acl9 specific questions. It seems like you're actually
struggling with some basic Rails features - and I remember you saying
that you were new to Rails, so that makes sense.

This is the reason people here are a bit confused, trying to work out
what problem you're having. You're not actually having an Acl9
problem - in fact it sounds like you know precisely what to do once
you get the values in from the form.

I'd recommend finding a Rails forum like http://groups.google.com/group/rubyonrails-talk
or #rubyonrails on Freenode.

Eric Kurzenberger

unread,
Nov 19, 2009, 10:15:50 AM11/19/09
to acl9-discuss
Thanks for the advice, Jason. I originally came here because I was
having difficulty figuring out the acl9 methods to use to list the
available roles and pass the role selected back to the database. I'll
check out that forum and see if I can get some assistance there. This
is my first experience with Rails, so it's probably just something
obvious that I'm missing.

Cheers,

Eric


On Nov 18, 1:30 pm, Jason King <smathy.w...@gmail.com> wrote:
> Eric,
>
> There are not Acl9 specific questions.  It seems like you're actually  
> struggling with some basic Rails features - and I remember you saying  
> that you were new to Rails, so that makes sense.
>
> This is the reason people here are a bit confused, trying to work out  
> what problem you're having.  You're not actually having an Acl9  
> problem - in fact it sounds like you know precisely what to do once  
> you get the values in from the form.
>
> I'd recommend finding a Rails forum likehttp://groups.google.com/group/rubyonrails-talk
Reply all
Reply to author
Forward
0 new messages