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