Home arrow Ruby-on-Rails arrow Action Pack Categories and Events

Action Pack Categories and Events

In this fourteenth part of a multi-part series on the Action Pack library, you'll learn how to add categories to the Events form and handle categories in the Events controller. 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 / 3
July 06, 2011
  1. · Action Pack Categories and Events
  2. · Handling Categories in the Events Controller

print this article

Action Pack Categories and Events
(Page 1 of 2 )

Adding Categories to the Events Form

In Chapter 5, we added categories to the Event model, but neither our controller nor our templates know about this yet. Let’s remedy that now, starting with the events form. Add the code shown in bold in Listing 6-18 to the form partial in app/views/events/_form.rhtml .

Listing 6-18. Modified app/views/events/_form.rhtml

<%= error_messages_for 'event' %>

<p><br/> <%= text_field 'event', 'title' %></p>

<p><br/> <%=

select_tag( 'categories[]', options_for_select(

Category.find(:all).collect {|c| [c.name, c.id] },

@event.category_ids ), :multiple => true

) %></p>

<p><br/> <%= text_field 'event', 'location' %></p>

<p><br/> <%= date_select 'event', 'occurs_on' %></p>

<p><br/> <%= text_field 'event', 'url' %></p>

<p><br/> <%= text_area 'event', 'description', :rows => 5 %></p>

Note In order to fit within the page margins, we’ve reformatted the code for the select_tag method. It looks a little unwieldy, but bear with us.

The select_tag method deserves a closer look.

select_tag( 'categories[]', options_for_select(

Category.find(:all).collect {|c| [c.name, c.id] }, @event.category_ids ), :multiple => true


The select_tag method is another of Action View’s form helpers. It creates a drop-down selection box, or if the :multiple option is set to true (as it is in our case), a multiple-choice selection box. By including a set of square brackets with the tag’s name ( categories[] ), we’re telling Rails to generate a collection of values in the params that are submitted.

It’s important to note that select_tag generates only the HTML select tag. We need to generate the actual option tags ourselves. Again, Rails provides a helper to take care of the heavy lifting: options_for_select . The options_for_select helper accepts a collection and returns a string of option tags. Given a collection where the elements respond to first and last (such as a two-element array), the firsts serves as the option test, and the lasts serves as the option values. A second argument can be passed to options_for_select to specify any options that should be preselected. In our case, we’re passing in all the category_ids for the given event so they will be preselected. This is easily the most advanced helper we’ve looked at so far, so it’s okay to take it on faith a bit. Once we add the appropriate code to handle categories in the controller, everything should “just work.”

You can find the full documentation for select_tag and options_for_select helpers in the Ruby API:

  1. http://api.rubyonrails.org/classes/ActionView/Helpers / FormTagHelper.html#M000604
  2. http://api.rubyonrails.org/classes/ActionView/Helpers/ FormOptionsHelper.html#M000510

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