I am new to RoR development and I am trying to set the checked value of
a
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 :
@all_ratings
@rating_flags = []
@all_ratings.each do |rating|
if @selected_ratings.include?(rating)
@rating_flags << “checked = true”
else
@rating_flags << “checked = false”
end
end
My view is the following:
-
@all_ratings.each do |rating|
= rating
= check_box_tag(“ratings[#{rating}]”, value = 1,
“#{@rating_flags[@all_ratings.index(rating)]}”)
= 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:
I have not added anything to the model it is just inheriting from
ActiveRecord::Base.
It looks as if the values are being saved in the controller, because if
I
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
boxes.
Any help would be greatly appreciated.
Thanks