public
Description: acts_as_authenticated Ruby on Rails authentication system
Home | Edit | New

Useful Code Snippets

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

Member

, there should be a

current_member
Last edited by gundestrup, Fri Oct 24 03:34:41 -0700 2008
Home | Edit | New
Versions: