Home arrow Ruby-on-Rails arrow Page 2 - Logging Out, Events and Templates with Action Pack

Logging Out, Events and Templates with Action Pack

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).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
June 27, 2011
  1. · Logging Out, Events and Templates with Action Pack
  2. · Improving the Events Controller and Templates

print this article

Logging Out, Events and Templates with Action Pack - Improving the Events Controller and Templates
(Page 2 of 2 )

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.

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials