【问题标题】:Rails - Using Metasearch / Ransack with a single textfieldRails - 将 Metasearch / Ransack 与单个文本字段一起使用
【发布时间】:2012-08-04 00:25:53
【问题描述】:

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

【问题讨论】:

  • 具体来说,使用 :attrib1_or_attrib2_condition 就像@Danny 在他的第一个 text_field 中所做的那样。

标签: ruby-on-rails search


【解决方案1】:

当然可以。以下代码包含 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

【讨论】:

    猜你喜欢
    • 2014-03-02
    • 2012-02-06
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    相关资源
    最近更新 更多