Reputation: 2647
I have some checkboxes on a HAML doc, which should change the results displayed when I click the 'refresh' button. This works fine, but when the page reloads the boxes that were checked are all unchecked again.
How do I reconfigure the HAML to persist the checkboxes checked state across page views?
= form_tag movies_path, :method => :get do
Include:
- @all_ratings.each do |rating|
= rating
= check_box_tag "ratings[#{rating}]", '1', true
= submit_tag 'Refresh'
I have also specified that the checkboxes should be checked by default, but they aren't checked when I load the page...
Upvotes: 3
Views: 5450
Reputation: 21
supernova32 was very close.
The only thing missing was based on your last line "...specified that checkboxes should be checked by default."
Your code should look like this:
= check_box_tag "ratings[#{rating}]", 1, if params[:ratings]; params[:ratings].include?(rating) else true end
Just include the ELSE statement for the times when the param does not exist (the default state.)
Upvotes: 2
Reputation: 321
This will accomplish what you want:
= form_tag movies_path, :method => :get do
Include:
- @all_ratings.each do |rating|
= rating
= check_box_tag "ratings[#{rating}]", rating, if params[:ratings]; params[:ratings].include?(rating) end
= submit_tag 'Refresh'
The params[:ratings]
instance persists after the call, so you can just use it to mark the boxes that where clicked by the user before.
Upvotes: 5
Reputation: 16435
I suppose the checkboxes do not represent some persistent state of your business data (otherwise this wouldn't apply) but they are in fact a sort of "filters" or similar to temporarily configure the view.
In that case, you should request with AJAX only the results, and not the checkboxes. So you don't have to store them anywhere, as they don't go away.
Upvotes: 0