Forum: Ruby on Rails LoginGeneratorACLSystem Issue

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cf17040cecb2618506f5ca4c14957957?d=identicon&s=25 Robert Dempsey (flatekmonkey)
on 2006-04-14 22:29
Hello all,

Again, thank you all in advance for any help here. I am trying to figure
out how to show the role associated with a user, in a template. I am
using the LoginGeneratorACLSystem along with the LoginGenerator. I can
show the first name of the user by writing "<%=
session[:user].first_name %>" but when I write "<%=
session[:user].roles.name %>" it spits out "Role". I am not sure what I
am doing wrong.

Essentially what I am trying to do is lock down user actions based on
what role they have. If anyone know of a "better" way of doing it than
using LoginGenerator along with the LoginGeneratorACLSystem I appreciate
the input.

Sincerely,

Robert Dempsey
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (Guest)
on 2006-04-15 03:49
(Received via mailing list)
Hey Robert, could you post the exact error message that you getting so
that
we can better assist you?

Thanks in advance,

-Conrad
Cf17040cecb2618506f5ca4c14957957?d=identicon&s=25 Robert Dempsey (flatekmonkey)
on 2006-04-15 05:09
Conrad Taylor wrote:
> Hey Robert, could you post the exact error message that you getting so
> that
> we can better assist you?
>
> Thanks in advance,
>
> -Conrad

Conrad,

There is not an error produced, per se, but rather a lack of correct
information showing. Here is the model code:

User Model:
  has_and_belongs_to_many :roles

Role Model:
  has_and_belongs_to_many :permissions
  has_and_belongs_to_many :users

Permissions Model:
  has_and_belongs_to_many :roles

Now, the permissions and roles definately work correctly, however, when
I insert this code, "<%= session[:user].roles.name %>" into the
"welcome.rhtml" page that is the default after a user logs in, this is
what show in the browser: "You are logged in as an Role". I would like
to show the actual role of the user, such as "Admin", etc.

Thank you for your help.

Sincerely,

Robert Dempsey
58c6efb8466b9f85155fe6aa9fc37fce?d=identicon&s=25 Chris T (Guest)
on 2006-04-15 09:48
(Received via mailing list)
I'm not familiar with LoginGenerator, but I think the problem is that
session[:user].roles will produce many answers (a user can have many
roles, right?)-- I think you should probably get an array of Role
objects returned. You'll need to iterate through that array to get the
roles the user has (which may only be one) -- something like:

    You are in the following group(s):
    <% for role in session[:user].roles %>
       <%= role.name %>
    <% end %>

It's always worth turning to the console when you have a strange error
like this (ruby script/console). Takes about 30 secs and gives you an
instant feedback on what sort of object is being returned. Hope this
helps.
Cf17040cecb2618506f5ca4c14957957?d=identicon&s=25 Robert Dempsey (flatekmonkey)
on 2006-04-15 13:56
(Received via mailing list)
Chris,

I will try this first thing today. Thank you for your help. I have been
using the console (to a limited extent) and have found it extremely
useful. I am also studying Ruby more so that I will stop asking so many
noob-like questions and will hopefully be able to help out more. Thanks
again.

- Rob D.
Cf17040cecb2618506f5ca4c14957957?d=identicon&s=25 Robert Dempsey (flatekmonkey)
on 2006-04-15 14:18
Chris,

Worked like a charm! Thank you again for your help.

- Rob D.
58c6efb8466b9f85155fe6aa9fc37fce?d=identicon&s=25 Chris T (Guest)
on 2006-04-15 14:39
(Received via mailing list)
Glad to have been able to help
This topic is locked and can not be replied to.