What''s the difference between: @current_role = current_user.try(:role) || :guest and @current_role = current_user.try(:role) || "guest" -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en.
On 25 Nov 2011 07:58, "Mauro" <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > What''s the difference between: > > @current_role = current_user.try(:role) || :guest > > and > > @current_role = current_user.try(:role) || "guest" >One sets @current_role as a string, the other as a symbol... Are you asking "what''s the difference between strings and symbols?" - if so, Google is your friend. As an aside, it may be cleaner code to not store the value in a new instance variable in your controller, but to have a method called current_role on the User model that does the same calculation. It''ll end up DRYer this way. (Actually, if it were me, i would probably put a default value if ''guest'' in the db field for current_user.role and validate the potential values to a list ...) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en.
On 25 November 2011 09:33, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On 25 Nov 2011 07:58, "Mauro" <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> What''s the difference between: >> >> @current_role = current_user.try(:role) || :guest >> >> and >> >> @current_role = current_user.try(:role) || "guest" >> > > One sets @current_role as a string, the other as a symbol... > > Are you asking "what''s the difference between strings and symbols?" - if so, > Google is your friend. > > As an aside, it may be cleaner code to not store the value in a new instance > variable in your controller, but to have a method called current_role on the > User model that does the same calculation. It''ll end up DRYer this way. > (Actually, if it were me, i would probably put a default value if ''guest'' in > the db field for current_user.role and validate the potential values to a > list ...)Yes you''re right but my application is for different uses. In admin use the user must be logged in, so he has a role assigned, etc. etc. In normal use the users do not to be logged in so there isn''t a user class but I need some information to display a different layout if the user is logged in and he is an admin or if I have no users logged. Sorry for my bad english. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en.
On 25 November 2011 09:11, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Yes you''re right but my application is for different uses.All applications are for different uses.> In admin use the user must be logged in, so he has a role assigned, etc. etc. > In normal use the users do not to be logged in so there isn''t a user > class but I need some information to display a different layout if the > user is logged in and he is an admin or if I have no users logged.Then have your "current_user" method return an unsaved user object for the not-logged-in-users - this will let you populate it with default values: def current_user User.find_by_id(session[:user_id]) || User.new(:name => "not logged in", :role => "guest", :etc => "etc") end Then you have a "logged_in?" method that checks for ".new_record?" : class User < AR:Base # all your stuff up here... def logged_in? !self.new_record? end end ...so now you can check "current_user.logged_in?" in your controllers and views to determine what content to show. M -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en.