No Ransack::Search object was provided to search_form_for

前端 未结 2 723
难免孤独
难免孤独 2021-01-06 06:23

I realise other people have asked about this error but it was to do with a different circumstance.

I have added the Ransack gem for rails 4 and bundled installed wi

2条回答
  •  -上瘾入骨i
    2021-01-06 06:44

    The @q object is only initialized when the request contains a "q" parameter.

    You should try to reduce the action index to the form of:

    def index
      @q = Recipe.search(search_params)
      @recipes = @q.result(distinct: true).paginate(page: params[:page], per_page: 10)
    end
    
    private
    
    def search_params
      default_params = {}
      default_params.merge({user_id_eq: current_user.id}) if signed_in?
      # more logic here
      params[:q].merge(default_params)
    end
    

提交回复
热议问题