How to check a check box in Haml using check_box_tag

前端 未结 6 1539
醉梦人生
醉梦人生 2020-12-17 17:18

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 mo         


        
相关标签:
6条回答
  • 2020-12-17 17:26

    Use true for Checked or false for Unchecked at the end of the line

    check_box_tag "ratings[#{rating}]", true #checked
    

    or

    check_box_tag "ratings[#{rating}]", false #unchecked
    
    0 讨论(0)
  • 2020-12-17 17:30
    = check_box_tag "ratings[#{rating}]",{},{:checked => ""}
    
    0 讨论(0)
  • 2020-12-17 17:33

    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.

    - Puppies.each do |puppy|
      = check_box_tag(puppy.name, puppy.name, puppy.goodboy?)
      = puppy.name
    
    0 讨论(0)
  • 2020-12-17 17:39

    Ref check_box_tag

    check_box_tag "ratings[#{rating}]",  1, !!(rating.rating)
    

    Your 2nd parameter must be value of checkbox

    Your 3rd parameter must be a boolean condition which return true/false and depends on it checkbox is checked/unchecked

    0 讨论(0)
  • 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:

    - if (some condition)
      = check_box_tag "ratings[#{rating}]", "anystring", true
    - else 
      = check_box_tag "ratings[#{rating}]" 
    

    The second line just puts a random string into the value field because in this case it does not matter.

    0 讨论(0)
  • 2020-12-17 17:49
    check_box_tag "ratings[#{rating}]",  1, @selected.include?("#{rating}")
    

    where @selected is an array with the element selected.

    0 讨论(0)
提交回复
热议问题