Checkbox Lists in Ruby on Rails

Posted by Mike on October 21, 2009

I will briefly cover creating a list of check boxes on a website that will allow data to be saved to the database. This can be done using a standard form and post or using Ajax. This article will cover the form/post method.

For this example I will have a feature model and a client model. There is a many to many relationship between these two models using a subscription model.

We’ll start with a view template that just lists all available features:

<ul>
  <% for feature in @features %>
    <li><%= feature.name %></li>
  <% end %>
</ul>

 

Now let’s put it inside a form, and checkbox to each list item, and put an update button on it:

<% form_tag "/path/to/post/to", :method => :put do %>
  <ul>
    <% for feature in @features %>
      <li>
        <%= check_box_tag 'feature_ids[]', feature.id, subscribed_to?(feature) %>
        <%= feature.name %></li>
    <% end %>
  </ul>
  <p>
  <%= submit_tag 'Update' %>
  </p>
<% end %>

OK, there are a couple things here that I will explain.  Notice the use of the square brackets in the check_box_tag name.  This allows there to be a list of items with the same name and have them passed as an array in the params.  Also notice that instead of stating true or false as the third element of the check_box_tag, I’ve used a helper method.  This allows me to say that the check box is checked if the client is subscribed to that feature.  That code could look something like:

def subscribed_to?(feature)
  @client.features.include?(feature)
end

We also need a little code in our controller to populate things:

def subscriptions
  @client = Client.find(params[:id])
  @features = Feature.all
end

The action used by the “/path/to/post/to” should now have everything it needs to update the database.  There is an array of feature_ids that can be passed to a model to update the database.  Then you might want to do something like set a flash message to let the user know what happened and redirect to an appropriate page.


def set_subscriptions
  do_something_with_this_array(params[:feature_ids])

  flash[:notice] = ‘Subscriptions Updated’

  redirect_to ‘/someplace/relevant’
end

This is not a complete tutorial by any means.  Hopefully it helps point you in the right direction.  Railscast #52 covers this same topic and may also be a helpful resource.

Ruby on Rails Development Environment on Ubuntu Linux 8.04.1 1

Posted by Mike on August 13, 2008

I just went through the process of setting up a new Linux desktop for Rails development.  In an effort to keep it short and to the point, I’ve trimmed up some of the info that I found useful from wiki.rubyonrails.org.  This is just a basic development stack.  I plan to write another post on the editors that I’ve tried and what I’ve found to work best for me so far.  Anyway, let’s get started.  I tend to work primarily at a command prompt, so that’s the method I’ve described here.  Open a terminal window and follow along:

sudo apt-get install ruby rdoc libyaml-ruby libzlib-ruby ri libopenssl-ruby ruby1.8-dev build-essential

You don’t want to install the rubygems package from the Ubuntu repository.  It is consistently out of date, so you will need to pull the tarball down from RubyForge:

wget http://rubyforge.org/frs/download.php/38646/rubygems-1.2.0.tgz
tar xvzf rubygems-1.2.0.tgz
cd rubygems-1.2.0
sudo ruby setup.rb

At least with the versions I’ve installed, I had to create a symbolic link to gem1.8:

sudo ln -s /usr/bin/gem1.8 /usr/bin/gem

You should now be able to do a gem update:

sudo gem update --system

And install Rails:

sudo gem install rails

Now it’s time to take care of the database installation:

sudo apt-get install mysql-server libsqlite3-0 libsqlite3-dev
sudo gem install mysql sqlite3-ruby

If you want to install RMagick:

sudo apt-get install imagemagick libmagick9-dev
sudo gem install rmagick

That should do it.  Now go have some fun!