You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 14, 2026. It is now read-only.
gundestrup edited this page Sep 13, 2010
·
1 revision
Here are some helpful code snippets – how to check if a user is logged in, how to display conditional content based on the type of user or if they are logged in, etc.
If you have something you think would be useful to other people using these plugins, feel free to add to this list
Testing for a logged-in user in a view
Add comment
<% if logged_in? %>
<% else %>
You must be logged in to comment.
<% end %>
Conditional partials
<% if logged_in? %>
<%= render(:partial => “shared/sideNavUser”) %>
<% else %>
<%= render(:partial => “shared/sideNavLogin”) %>
<% end %>
Testing for a logged-in user in a view (alternative)
I have had issues with using the ’@’ instance variable notation inside of views. So, for example, @current_user never seems to work for me. However, ‘self’ works instead. So, I use the following snippet inside views:
Welcome user
<% if logged_in? %>
Welcome, <%= self.current_user.login %>
<% else %>
<% end %>
Alternate for current user
current_user
(or whatever your accounted model is named) should work.
So, if your model is