symbol or string?

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.

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