有谁知道是否可以将 Metasearch / Ransack gem 与单个文本字段而不是多个文本字段一起使用?对于我的应用程序,我只想在我的模型中搜索两个关联的属性,而全文搜索似乎有点矫枉过正。无论如何,任何帮助将不胜感激!
问问题
1476 次
1 回答
0
是的当然。以下代码包含 4 个字段,但您可以只有一个。这是一个很好的资源(railscast),但很快就会进入您试图避免的搜索的高级方面:http ://railscasts.com/episodes/370-ransack?autoplay=true
控制器
def search
@search = Entry.search(params[:q])
@entries = @search.result
end
查看 (haml)
%h1 Search
= search_form_for @search, :url=>search_entries_path do |f|
.field
=f.label :make_or_model_cont, "Make or Model"
=f.text_field :make_or_model_cont
.field
=f.label :last_four_cont, "Last Four contains"
=f.text_field :last_four_cont
.field
=f.label :created_at_gt, "Date after"
=f.text_field :created_at_gt, :class=>"scroller time_field" # using jquery datetime picker plugin
.field
=f.label :created_at_lt, "Date is before"
=f.text_field :created_at_lt, :class=>"scroller time_field"
.actions
=f.submit "Search"
=render 'results' # contains table, ths and tr + tds to hold the data
于 2012-08-18T04:25:01.853 回答