How to pre-check checkboxes in formtastic

前端 未结 4 825
天命终不由人
天命终不由人 2021-02-04 03:14

I have a form I\'m trying to set up ... Users can have many posts, and each post can have many people watching it.

The Watch model is set up polymorphically as \'watcha

相关标签:
4条回答
  • 2021-02-04 03:42

    For anyone else having the same issue:

    <%= f.input :some_input, :as => :boolean, :input_html => { :checked => 'checked' } %>
    
    0 讨论(0)
  • 2021-02-04 03:43

    If you need the state of the checkbox to reflect the value of :some_input

    <%= form.input :some_input, :as => :boolean, :input_html => { :checked => :some_input? } %>
    

    In your model..

    def some_input?
      self.some_input ? true : false
    end
    
    0 讨论(0)
  • 2021-02-04 03:53

    Set the boolean attribute's value to 'true' in the controller before your render the form. That should make Formtastic default the checkbox to 'checked'.

    0 讨论(0)
  • 2021-02-04 03:55

    For multiple check boxes, this way:

    <%= f.input :tags, :as => :check_boxes, :collection => some_map.collect { |c| [c[:name], c[:id], {:checked=> tag_ids.include?(c[:id])}] } %>
    
    0 讨论(0)
提交回复
热议问题