Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep check boxes checked in HAML

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...

like image 559
skeniver Avatar asked Dec 11 '25 14:12

skeniver


1 Answers

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.

like image 61
supernova32 Avatar answered Dec 13 '25 02:12

supernova32



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!