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 -- Posted via http://www.ruby-forum.com/.
Hey Robert, could you post the exact error message that you getting so that we can better assist you? Thanks in advance, -Conrad On 4/14/06, Robert Dempsey <rdempsey@techcfl.com> wrote:> > 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060415/86ba110f/attachment.html
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, > > -ConradConrad, 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 -- Posted via http://www.ruby-forum.com/.
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. Robert Dempsey wrote:> 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 > >
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. Chris T wrote:> 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.
Chris, Worked like a charm! Thank you again for your help. - Rob D. -- Posted via http://www.ruby-forum.com/.