Can someone tell me how to set these check boxes to checked? I'm sure it's simple, but after an hour of trying i think I need to ask! Thanks!
= form_tag movies_path, :id => 'ratings_form', :method => :get do
Include:
- @all_ratings.each do |rating|
= rating
= check_box_tag "ratings[#{rating}]",
= submit_tag 'Refresh', :id => 'ratings_submit'
Ref check_box_tag
Your
2nd parameter
must bevalue
of checkboxYour
3rd parameter
must be aboolean condition
which returntrue/false
and depends on it checkbox ischecked/unchecked
According to the api dock, check box tag takes the following options:
check_box_tag(name, value = "1", checked = false, options = {})
This means the first value is the name, the second value is a 'value' and the third value is whether the box is checked, which is default to false. So, in order to check or uncheck the box you can do the following:
The second line just puts a random string into the value field because in this case it does not matter.
Use true for Checked or false for Unchecked at the end of the line
or
Building on the answer by Sali. Strangely, the
check_box_tag
returns a checkbox with no label text. Here's how you can display text if you're iterating over an array.where
@selected
is an array with the element selected.