Using select Helper in Rails

In my last post I wrote that I had produced a PHP application for the company that I currently work for. Well, now that I’m trying to learn Ruby on Rails I decided that I would convert that applicition from PHP to Rails. I’ve not really allocated very much time to the task at the moment but hope I can give it some more of my time soon.

One of the things that the PHP application had was a number of drop-down combo-boxes (or selection lists) on the main page. These were used to filter the displaying of problems that the system was tracking. The user could use these selection lists to get the app. to show only problems that they had raised for example, or to only show problems that related to a particular system. This app. was being used to track problems with a large piece of software that we had recently delivered to our customer. Without going into details, the software was focused on the training of ground-crew who maintain fast jets. This meant that problems tracked by the app. would relate to various aircraft system, engine, communications, navigation, etc.

Anyway, back to the selection lists. As I have a table in my database listing all of the aircraft systems I could have simply populated my selection list with the systems from that table/model quite simply:

In the controller:

@system_names =  System.find(:all, :order => 'description')

In the view:

<% form_tag do %>
      <label for="system_id">System Title to filter on:</label>
      <%= select_tag(:system_id,
         :id, :description) %>
   <%= submit_tag 'Filter Issues' %>
<% end %>

However I wanted to ensure that the option selected by the user would remain selected and I also wanted to add an option to the beginning of the list that could be used to switch the filter off. I thought at the beginning of the week that this would be a really easy task but I spent several days of frustration trying different options, looking at the online Rails Framework Documentation and searching a Rails Forum or two. I was about to give up and submit a post to one of the forums when I had a breakthrough.

I finally worked out what I needed to add to the options_from_collection_for_select helper method to get the previously selected option to appear selected if the form was submitted:

<%= select_tag(:system_id,
   :id, :description, params[:system_id].to_i) %>

It was some time before I realised that I needed to add to_i to convert the string stored in params to an integer. I guess that I had assumed that this conversion would take place automatically. This highlights one of my problems learning Rails and that’s the fact that I haven’t really got any experience using Ruby and I’m beginning to think that is a mistake.

As I write this post I’m realising that I don’t actually need to have the previously selected option selected in the selection list using this technique. Instead I’ll simply find out what the user had selected and store it in a session variable; the use of params will only work if the user is submitting a form that needs to be re-displayed because of an error in the form, for example.

Adding an option to the beginning of the selection list took me longer to achieve and I don’t know if I’ve done it the best or easiest way. I’m just glad that I got it worked out eventually! I dropped the options_from_collection_for_select helper method in favour of the options_for_select method:

<%= select_tag( :system_id,
   params[:system_id].to_i) ) %>

In the controller I was toying with the idea of using the find method in the same way, to grab all available systems, and then adding the new option to the beginning of the list. Again, I had a number of problems with this due to my limited knowledge of Ruby as well as my inexperience at using Rails. Eventually I came to the following solution, where I convert the array of objects that is returned by the find method to an array of arrays (using the map method) and then joining this to another array of arrays that contains my first option:

@system_names =  System.find(:all, :order => 'description')
@system_names = [ ["No Filter Selected", 0] ]
   + {|p| [ p.description, ] }

It actually appears to work.


~ by gblake on March 17, 2008.

2 Responses to “Using select Helper in Rails”

  1. Good post. Did you think about using the select helper in another way? Instead of using the select_tag helper you could try messing around with the select helper. I just wrote a blog on it .

  2. It’s been a while since I wrote that code. I may have looked into using a select helper at the time but I can’t quite remember now why I didn’t!!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: