我正在使用Ransack进行搜索,这工作正常。但我希望用户能够在同一搜索输入字段中搜索多个关键字。
例如,如果我有博客“我住在俄亥俄州”,我希望用户能够搜索“我俄亥俄州”,它会找到博客。我尝试了以下不起作用:
<%= search_form_for @search do |f| %>
<%= f.search_field :title_or_description_cont_any %>
<% f.submit %>
<% end %>
这是我的控制器:
def index
@search = Blog.ransack(params[:q])
@blogs = @search.result
end
我原以为cont_any会起作用,但出于某种原因却不行?
更新:我也使用active_admin,我不知道这是否重要?
我认为应该如下
def index
@search = Blog.ransack(params[:q])
@blogs = @search.result(distinct: true)
end
你的表格应该是这样的,
<%= search_form_for @q do |f| %>
<%= f.search_field :title_or_description_cont %>
<% end %>
Title
和description
需要是您数据库中的实际字段。例如,如果您创建了一个包含header
,subtitle
和shortDescription
的字段的Post模型,则需要使用这些字段而不是title
和description
。
<%= f.input :title_or_description_cont_any %>
<%= f.input :title_or_description_cont %>
这样的事情:
<%= f.input :header_or_shortDescription_cont_any %>