No Ransack::Search object was provided to search_form_for

杀马特。学长 韩版系。学妹 提交于 2019-12-01 07:39:48

Nicolas was right in that the error is coming from @q only being initialized when the request contains a "q" parameter. Which is why before you submit the form you get the error (no "q" parameter).

another way to get around this is initializing @q

in your application_controller

def set_search
@q=Recipe.search(params[:q])
end

in your recipes_controller before_filter :set_search

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!