Rails - 使用Metasearch / Ransack和单个文本字段

时间:2012-08-04 00:25:53

标签: ruby-on-rails search

有没有人知道是否可以将Metasearch / Ransack gem用于单个文本字段而不是多个文本字段?对于我的应用程序,我只想在我的模型中搜索两个相关属性,全文搜索似乎有点矫枉过正。无论如何,任何帮助将不胜感激!

1 个答案:

答案 0 :(得分: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