简体   繁体   中英

Rails - Using Metasearch / Ransack with a single textfield

Does anyone know if its possible to use the Metasearch / Ransack gem with a single textfield instead of multiple textfields? For my app, I just want to search two associated attributes in my model and full text searching seems like overkill. Anyways, any help would be much appreciated!

Yes of course. The following code holds 4 fields, but you can just have one. Here's a good resource (railscast), but quickly gets into the advanced side of the search which you are trying to avoid: http://railscasts.com/episodes/370-ransack?autoplay=true

Controller

def search
  @search = Entry.search(params[:q])
  @entries = @search.result
end

View (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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM