###前提・実現したいこと
ransackを利用して、チェックボックスの検索機能を実現したいです。
###発生している問題
テキストフィールドやセレクトボックスの検索はできるのですが、チェックボックスの実装だけができません。
###該当のソースコード
ruby
1#モデル coordinator.rb 2class Coordinator < ApplicationRecord 3 has_and_belongs_to_many :offices 4end
ruby
1#モデル office.rb 2class Office < ApplicationRecord 3 has_and_belongs_to_many :coordinators 4end
ruby
1# _search_form.html.erb 2<%= f.collection_check_boxes( :offices_id_in, Office.all, :id, :name) %>
ruby
1# coordinator_controller.rb 2def index 3 @coordinators = Coordinator.all 4 @q = Coordinator.ransack(params[:q]) 5 @coordinators = @q.result.includes(:offices) 6 end 7 8 def search 9 @q = Coordinator.search(search_params) 10 @coordinators = @q.result.includes(:offices) 11 end 12 13 def search_params 14 params.require(:q).permit(:name_cont, :section_id_eq, :offices_id_in) 15 end
###補足情報
検索ボタンを押した時のログ
Parameters: {"utf8"=>"✓", "q"=>{"name_cont"=>"ほげ", "offices_id_in"=>["", "1"], "section_id_eq"=>""}, "commit"=>"検索"}
検索ボタンをクリックした時のパラメーター
search?utf8=✓&q%5Bname_cont%5D=ほげ&q%5Boffices_id_in%5D%5B%5D=&q%5Boffices_id_in%5D%5B%5D=1&q%5Bsection_id_eq%5D=&commit=検索
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/10 10:10