加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 没有Ransack ::搜索对象已提供给search_form_f

发布时间:2020-12-17 02:22:01 所属栏目:百科 来源:网络整理
导读:我意识到其他人已经问过这个错误,但这与其他情况有关. 我已经为rails 4添加了Ransack gem,并且已经安装了捆绑: gem "ransack",github: "activerecord-hackery/ransack",branch: "rails-4" 我还编辑了我的控制器如下(recipes_controller): def indexif para
我意识到其他人已经问过这个错误,但这与其他情况有关.

我已经为rails 4添加了Ransack gem,并且已经安装了捆绑:

gem "ransack",github: "activerecord-hackery/ransack",branch: "rails-4"

我还编辑了我的控制器如下(recipes_controller):

def index
if params[:tag]
  @all_recipes = Recipe.tagged_with(params[:tag])
else
  @all_recipes = Recipe.all
end
if signed_in?
  @user_recipes = current_user.recipes.order("created_at DESC").paginate(page: params[:page],:per_page => 10)
end
if params[:q]
  @q = Recipe.search(params[:q])
  @all_recipes = @q.result(distinct: true)
end
end

然后我在表单中添加如下(食谱/索引):

<%= search_form_for @q do |f| %>
  <%= f.label :name_cont %>
  <%= f.text_field :name_cont %>
  <%= f.submit %>
<% end %>

我收到以下错误:

No Ransack::Search object was provided to search_form_for!

在这条线上:

<%= search_form_for @q do |f| %>

这会与安装有关吗?

解决方法

Nicolas是正确的,因为当请求包含“q”参数时,错误来自@q仅被初始化.这就是为什么在您提交表单之前您会收到错误(没有“q”参数).

解决这个问题的另一种方法是初始化@q

在你的application_controller中

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

在你的recipes_controller中before_filter:set_search

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读