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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Michael Pavling at Nov 25, 2011 at 8:33 am

    On 25 Nov 2011 07:58, "Mauro" 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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Mauro at Nov 25, 2011 at 9:12 am

    On 25 November 2011 09:33, Michael Pavling wrote:
    On 25 Nov 2011 07:58, "Mauro" 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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Michael Pavling at Nov 25, 2011 at 9:45 am

    On 25 November 2011 09:11, Mauro 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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedNov 25, '11 at 7:58a
activeNov 25, '11 at 9:45a
posts4
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Michael Pavling: 2 posts Mauro: 2 posts

People

Translate

site design / logo © 2022 Grokbase