1
是否有人知道是否可以將Metasearch/Ransack gem用於單個文本字段而不是多個文本字段?對於我的應用程序,我只是想在我的模型中搜索兩個關聯的屬性,而全文搜索看起來像是矯枉過正。無論如何,任何幫助將不勝感激!Rails - 在單個文本字段中使用Metasearch/Ransack
是否有人知道是否可以將Metasearch/Ransack gem用於單個文本字段而不是多個文本字段?對於我的應用程序,我只是想在我的模型中搜索兩個關聯的屬性,而全文搜索看起來像是矯枉過正。無論如何,任何幫助將不勝感激!Rails - 在單個文本字段中使用Metasearch/Ransack
當然可以。以下代碼包含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
具體來說,使用:attrib1_or_attrib2_condition作爲@Danny在他的第一個text_field中執行的操作。 – 2012-11-09 02:17:19