【发布时间】:2014-02-16 13:39:36
【问题描述】:
我知道其他人问过这个错误,但它与不同的情况有关。
我添加了 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| %>
这与安装有关吗?
【问题讨论】:
-
感谢@emaillenin 的编辑。有什么建议吗?
-
用“ransack”替换“search”能解决问题吗? def index @q = Recipe.ransack(search_params) @recipes = @q.result(distinct: true).paginate(page: params[:page], per_page: 10) end
标签: ruby-on-rails ruby-on-rails-4 ransack