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:
<form accept-charset="UTF-8" action="/movies"
id="ratings_form"
method="get"><div
style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden"
value="✓" /></div>
Include:
G
<input checked="checked" id="ratings_G"
name="ratings[G]"
type="checkbox" value="1" />
PG
<input checked="checked" id="ratings_PG"
name="ratings[PG]"
type="checkbox" value="1" />
PG-13
<input checked="checked" id="ratings_PG-13"
name="ratings[PG-13]"
type="checkbox" value="1" />
R
<input checked="checked" id="ratings_R"
name="ratings[R]"
type="checkbox" value="1" />
NC-17
<input checked="checked" id="ratings_NC-17"
name="ratings[NC-17]"
type="checkbox" value="1" />
<input id="ratings_submit" name="commit"
type="submit"
value="Refresh" />
</form>
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
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/seFODUoFXGUJ.
For more options, visit https://groups.google.com/groups/opt_out.