在Rails 5中遇到Ransack查询问题和我的强参数

时间:2018-05-22 14:47:11

标签: ruby-on-rails ruby-on-rails-5 strong-parameters ransack

我有一个控制器 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

我在没有任何与之相关的模型的情况下使用它。我正在尝试使用它与hansack但我在运行搜索时在我的终端中不断收到以下错误。我似乎无法弄清楚如何获得“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>

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

更改您的搜索参数

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:[]中添加您想要允许的所有其他参数,如示例

中所示