Topic: Storing CheckBox Values To Datastore


I ran into some interesting behavior.  I have a form with some checkboxes on it.  I have a database table attribute that corresponds to each checkbox value.  So, if the box is checked,
I want to store 1 in the attribute.  If unchecked, I want to store 0 in the attribute.  I noticed that if the checkbox is unchecked, then there is no attribute/value for that checkbox passed in the params list to the controller.  Right now, I have a solution for this in that I just populate a hidden input value with 0 or 1 and pass that.  However, I have a bunch of these check boxes and for each one, I need to create a hidden input value.

A nicer solution would be to check in the controller whether the attribute/value pair exists
in the params list.  If not, then I could manually populate the object attribute so that it gets saved to the database.

Here is an example of my create method and a comment of the code I need.  What is the line of code that I need here?

def create
    @assemblytest =[:assemblytest])
    @assemblytest.assembly_id = cookies[:selectedAssemblyID]

    # How do I check the existence of a param here in the @params[:assemblytest] list?

        redirect_to :action => 'list'
        render_action 'new'

Re: Storing CheckBox Values To Datastore

It would probably be easier to set a default value of 0 in the database for that column. You can do that in your ActiveRecord migrations.

class AddStuff < ActiveRecord::Migration
  def self.up
    create_table :stuff do |t|
      t.column :test, :boolean, :default => 0

  def self.down


vinnie - rails forum admin

Re: Storing CheckBox Values To Datastore


That won't work.  What if the user checks a box, saves, and then unchecks it, then saves.

I really only need a way to check if a parameter does not exist.  Here is my parameter list.:

Parameters: {"assemblytest"=>{"testdate"=>"", "chkvalve1_heldatpsid"=>"", "inspector_id"=>"1", "chkvalve1_closedtight"=>"0", "reliefvalve_openedat"=>"", "nexttestdate"=>""}}

So, I just want to do something like 

if (@params[:assemblytest=>chkvalve1_leaked] == nil)

I just can't get the right syntax....there has got to be an easy way to do this.


Re: Storing CheckBox Values To Datastore

Try this:

params[:assemblytest][:chkvalve1_heldatpsid] ||= 0
params[:assemblytest][:chkvalve1_closedtight] ||= 0
params[:assemblytest][:chkvalve1_leaked] ||= 0

This will set the value to 0 if it is nil. If you have an array of values to check you can do this:

[:chkvalve1_heldatpsid, :chkvalve1_closedtight, :chkvalve1_leaked].each do |check_box|
  params[:assemblytest][check_box] ||= 0

Of course, you can move the array out of the controller and fetch, say, all of the boolean columns from the model.

Railscasts - Free Ruby on Rails Screencasts

Re: Storing CheckBox Values To Datastore


Excellent!...I used your suggestion to loop through all the designated checkboxes.  Works great
and simplifies my view code.

Thanks again,


Re: Storing CheckBox Values To Datastore

BTW, if you use the check_box helper method it will automatically generate the hidden field for you. Don't know if you want this solution or not, but it's there.

Railscasts - Free Ruby on Rails Screencasts