Check Bok Tag Checked Issue

I am new to RoR development and I am trying to set the checked value of
check_box_tag field, but no matter what I set the checked value to I get
the box checked.

My controller is as follows:

@all_ratings = ['G', 'PG', 'PG-13', 'R', 'NC-17']
@selected_ratings = params[:ratings] ? params[:ratings].keys :

@rating_flags = []
@all_ratings.each do |rating|
if @selected_ratings.include?(rating)
@rating_flags << “checked = true”
@rating_flags << “checked = false”

My view is the following:

  • @all_ratings.each do |rating|
    = rating
    = check_box_tag(“ratings[#{rating}]”, value = 1,
    = submit_tag ‘Refresh’, :id => ‘ratings_submit’

When the @rating_flags comes from the controller it has the following
values according to the debugger:

[“checked = false”, “checked = true”, “checked = false”, “checked =
false”, “checked = false”]

And the values of the check boxes are as follows:

Include: G PG PG-13 R NC-17

I have not added anything to the model it is just inheriting from

It looks as if the values are being saved in the controller, because if
make changes to the check boxes and refresh I get the expected result.

The only thing that does not work is the checked value of the check

Any help would be greatly appreciated.


On Sunday, October 21, 2012 1:56:34 PM UTC+1, Stephen Harnois wrote:

  • @all_ratings.each do |rating|
    = rating
    = check_box_tag(“ratings[#{rating}]”, value = 1,

Rails is expecting a truthy value: false or nil if the box shouldn’t be
checked, or anything else to make the box checked. It isn’t expecting an
html attribute string. You’re passing a string which always counts as
since it is neither falsde nor nil. If you
use @selected_ratings.include?(rating) instead that should do the trick.