在 Rails 5 中,Ransack 查询和我的强参数有问题

In Rails 5 having issues with Ransack query and my Strong Parameters

我有一个控制器,Players::SearchesController

class Players::SearchesController < ApplicationController

  def index
    @q = Player.ransack(search_params[:q])
    @players = Player.none
  end

  def create
    # This executes when I initiate a ransack search
  end

  private

    def search_params
      params.permit(:utf8, :commit, :search_type, :current_user_id, :q)
      params.require(:q).permit!
    end

end

我在使用它时没有关联任何模型。我正在尝试将它与 ransack 一起使用,但是当 运行 搜索时,我的终端中不断出现以下错误。我似乎无法弄清楚如何让 "q" 参数被允许。

(byebug) search_params
Unpermitted parameter: :q
<ActionController::Parameters {"position_id_in"=>["", "5"], "draft_year_in"=>["", "2019"], "measurable_summary_height_gteq"=>"5080", "measurable_summary_height_lteq"=>"5080", "measurable_summary_weight_gteq"=>"", "measurable_summary_weight_lteq"=>"", "measurable_summary_speed_gteq"=>"", "measurable_summary_speed_lteq"=>"", "measurable_summary_ten_yard_dash_gteq"=>"", "measurable_summary_ten_yard_dash_lteq"=>"", "measurable_summary_test_gteq"=>"", "measurable_summary_test_lteq"=>"", "measurable_summary_hand_span_gteq"=>"", "measurable_summary_hand_span_lteq"=>"", "measurable_summary_arm_length_gteq"=>"", "measurable_summary_arm_length_lteq"=>"", "measurable_summary_vertical_jump_gteq"=>"", "measurable_summary_vertical_jump_lteq"=>"", "measurable_summary_bench_reps_gteq"=>"", "measurable_summary_bench_reps_lteq"=>""} permitted: true>

这也是我的 params 对象的组成部分...

<ActionController::Parameters {"utf8"=>"✓", "q"=><ActionController::Parameters {"position_id_in"=>["", "5"], "draft_year_in"=>["", "2019"], "measurable_summary_height_gteq"=>"5080", "measurable_summary_height_lteq"=>"5080", "measurable_summary_weight_gteq"=>"", "measurable_summary_weight_lteq"=>"", "measurable_summary_speed_gteq"=>"", "measurable_summary_speed_lteq"=>"", "measurable_summary_ten_yard_dash_gteq"=>"", "measurable_summary_ten_yard_dash_lteq"=>"", "measurable_summary_test_gteq"=>"", "measurable_summary_test_lteq"=>"", "measurable_summary_hand_span_gteq"=>"", "measurable_summary_hand_span_lteq"=>"", "measurable_summary_arm_length_gteq"=>"", "measurable_summary_arm_length_lteq"=>"", "measurable_summary_vertical_jump_gteq"=>"", "measurable_summary_vertical_jump_lteq"=>"", "measurable_summary_bench_reps_gteq"=>"", "measurable_summary_bench_reps_lteq"=>""} permitted: true>, "search_type"=>"measurables", "commit"=>"Search", "controller"=>"players/searches", "action"=>"create"} permitted: false>

我哪里做错了?

像这样更改您的搜索参数

def search_params
  params.permit(:utf8, :commit, :search_type, :current_user_id, q: [:position_id_in, :draft_year_in.......])
  params.require(:q).permit!
end

并在 q:[] 中添加您想要允许的所有其他参数,如示例