In this thirteenth part of a multi-part series on the Action Pack library for Rails, you'll learn how to log out a user, clean up the events controller, and more. 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 (Apress; ISBN: 1590596862).



Logging Out a User


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 method reset_session 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.

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. 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 %> > </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.

Please check back regularly for the continuation of this series.