LoginSignup
22
17

More than 5 years have passed since last update.

collection_check_boxesでhiddenタグを挿入されないようにする方法

Posted at

個人的なメモ

railsのフォーム画面で、collection_check_boxesを使ってcheckboxのデータを配列で受け取りたかったが、
最後にhiddenタグが差し込まれるので配列の最後に空の文字列が入り込んでしまっていたので
hiddenタグが挿入されないようにしたかった。

結論としては以下のようにinclude_hidden: falseオプションを指定したら良い。

<%= f.collection_check_boxes :topic_id, Topic.all, :id, :name, include_hidden: false %>

ドキュメントにはない模様なので、いつか使えなくなるかもしれないが取り急ぎ。

参考:
http://stackoverflow.com/questions/20744435/by-default-simple-form-collection-check-boxes-displays-hidden-field-at-the-end

ruby: 2.3.1
rails: 4.2.6

22
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
17