1
2

More than 3 years have passed since last update.

【rails】enumを利用したラジオボタン検索【ransack】

Last updated at Posted at 2020-01-02

enumを利用したransackのラジオボタン検索で少しつまずいたので備忘録用として投稿

ransackの導入やenumの書き方については端折ります。

micropost.rb
enum result: { win: 1, lose: 2, notrade: 3 }

検索できなかった時

_serch_form.html.slim
  = search_form_for @q do |f|
        h2.my-3 検索
        = f.label :result
        br.form-check.form-check-inline
        = f.radio_button :result_eq, :win, {:checked => true}
        = f.radio_button :result_eq, :lose
        = f.radio_button :result_eq, :notrade
        br
        = f.submit nil, class:'btn btn-info'

正しいやつ

_serch_form.html.slim
  = search_form_for @q do |f|
        h2.my-3 検索
        = f.label :result
        br.form-check.form-check-inline
        = f.radio_button :result_eq, 1, {:checked => true}
        = f.radio_button :result_eq, 2
        = f.radio_button :result_eq, 3
        br
        = f.submit nil, class:'btn btn-info'

インデントとか気にしないで下さい。

なぜか最初はハッシュを入力していました。

1
2
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
1
2