[英]Ruby on Rails ransack gem search
i want to implement a search that goes through multiple models. 我想实现一个通过多个模型的搜索。
Found this stackoverflow question here that uses ransack and tried it right away. 在这里找到了这个使用ransack的stackoverflow问题 ,并立即对其进行了尝试。 But I can't seem to get it to work.
但是我似乎无法使其正常工作。
in my controller : 在我的控制器中:
def search
@quotes = Quote.search(title_cont: q).result
@books = Book.search(title_cont: q).result
@users = User.search(username_cont: q).result
end
routes 路线
get '/search', to: 'application#search'
view 视图
<%= form_tag search_path, method: :get do %>
<%= f.label :title_cont %>
<%= f.search_field :title_cont %>
<%= text_field_tag :q, nil %>
<% end %>
You should use params[:q]
instead of q
. 您应该使用
params[:q]
而不是q
。 This should work 这应该工作
def search
@quotes = Quote.search(title_cont: params[:q]).result
@books = Book.search(title_cont: params[:q]).result
@users = User.search(username_cont: params[:q]).result
end
Also, f.label and f.search_field
doesn't work with form_tag
. 另外,f.label和
f.search_field
不适用于form_tag
。 You should use label_tag
and search_field_tag
instead 您应该改用
label_tag
和search_field_tag
<%= form_tag search_path, method: :get do %>
<%= label_tag :title_cont %>
<%= search_field_tag :title_cont %>
<%= text_field_tag :q, nil %>
<% end %>
Why do you have two fields in your search form, you should only have one search field. 为什么您的搜索表单中有两个字段,而您只应有一个搜索字段。
<%= form_tag search_path, method: :get do %>
<%= label_tag :title_cont %>
<%= search_field_tag :q %>
<% end %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.