Logging Out, Events and Templates with Action Pack

Action Pack in Ruby on Rails: Events, Templates & Logouts

In this thirteenth part of a multi-part series on the Action Pack library for Rails, you'll learn how to securely log out a user, clean up the events controller, and effectively use partial templates. Managing user sessions and keeping controllers DRY (Don't Repeat Yourself) are essential skills for any professional Ruby on Rails developer. This article is excerpted from chapter six of the book Beginning Rails: From Novice to Professional, written by Jeffery Allan Hardy, Cloves Carneiro Jr. and Hampton Catlin.

Logging Out a User in Rails

The logout action is fairly straightforward. We clear out the :user_id session by setting its value to nil, and then we redirect back to the login action.

def logout
  session[:user_id] = nil
  redirect_to login_url
end

This is a good way to clear out a specific session, but sometimes you want to clear the entire session without having to remember which keys to reset. Rails provides the built-in reset_session method to do just that.

We’ve done a lot of work on the users controller so far. Listing 6-14 shows what it should look like at this stage.

Listing 6-14. Actions Added in app/controllers/users_controller.rb

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
  end
  def new
    @user = User.new
  end
  def create
    @user = User.new(params[:user])
    if @user.save
      flash[:notice] = 'Thanks for signing up!'
      redirect_to :controller => 'events', :action => 'index'
    else
      render :action => 'new'
    end
  end
  def edit
    @user = User.find(params[:id])
  end
  def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      flash[:notice] = 'Information updated'
      redirect_to :action => 'show', :id => @user.id
    else
      render :action => 'edit'
    end
  end
  def login
    if request.post?
      if user = User.authenticate(params[:login], params[:password])
        session[:user_id] = user.id
        redirect_to :controller => 'events', :action => 'index'
      else
        flash[:notice] = 'Invalid login/password combination'
      end
    end
  end
  def logout
    session[:user_id] = nil
    redirect_to :action => 'login'
  end
end

In walking through the construction of the users controller, we’ve covered generating controllers, creating templates and layouts, and DRYing up with partials. Now let’s take that knowledge and apply it to the events controller (and templates).

Cleaning Up the Events Controller

Currently, our events controller is stock scaffolding. To be sure, scaffolding has its place; it served us quite well as a learning tool. But now it’s time to rework the scaffolded version for a production-ready Action Pack application.

Open the events controller (app/controllers/events_controller.rb) in your editor. Notice how the scaffolder creates index and list actions. Take a close look, and you’ll notice that the index action renders the list template. The two actions are pretty much identical, so there’s no need for both of them. Let’s make the index action render the list of events, as shown in Listing 6-15.

Listing 6-15. Index Action Updated in app/controllers/events_controller.rb

def index
  @event_pages, @events = paginate :events, :per_page => 10
end

Now delete the list action. This cuts down on the clutter. Also, using the index action as the “collection” action (wherein we display a list of resources) is a Rails convention not yet applied to the scaffold generator. We can apply another of the techniques you’ve learned—rendering a collection of partials—to further improve the code.

Using Partials in the Events Templates

As you learned when walking through the users controller, Rails includes a clever way of economizing on template code called partials. The pattern of rendering a collection of partials is particularly powerful within Action Pack. Let’s rename the list template of our events controller to index, and then rewrite it to take advantage of partials. Listing 6-16 shows the modified version.

Listing 6-16. Modified app/views/events/index.rhtml

<h1>All Events</h1>
<%= render :partial => 'event', :collection => @events %>
<%= link_to 'Previous page', { :page => @event_pages.current.previous } if @event_pages.current.previous %>
<%= link_to 'Next page', { :page => @event_pages.current.next } if @event_pages.current.next %>

Listing 6-17 shows the _event partial.

Listing 6-17. The app/views/events/_event.rhtml File

<div class="event">
  <h2><%= event.title %></h2>
  <ul>
    <li><%= event.occurs_on %></li>
    <li><%= event.location %></li>
  </ul>
  <div class="description">
    <%= event.description %>
  </div>
</div>

When this template is rendered, Rails will automatically iterate over the collection and create a div element for each event.

Frequently Asked Questions (FAQ)

How do you clear a user session in Ruby on Rails?

To clear a specific user session in Ruby on Rails, you can set the session key (like session[:user_id]) to nil. If you want to clear the entire session completely, you can use the built-in reset_session method to ensure all session data is wiped.

What are partials in Rails Action Pack?

Partials in Rails Action Pack allow you to break down complex view templates into smaller, reusable code snippets. They help in keeping your code DRY (Don't Repeat Yourself) by rendering recurring elements efficiently without duplicating markup.

Why consolidate index and list actions?

Consolidating index and list actions in a Rails controller reduces code duplication. Since both actions typically display a list of resources, rendering a collection of partials from a single index action aligns better with Rails conventions and keeps your codebase cleaner.