How to pre-check checkboxes in formtastic

旧城冷巷雨未停 提交于 2019-12-02 22:51:43
John Goodman

For anyone else having the same issue:

<%= f.input :some_input, :as => :boolean, :input_html => { :checked => 'checked' } %>
Muntasim

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])}] } %>

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!