ruby-on-railsrubyransackmeta-search

Is Ransack has a way to implement token search


Now I have search_field

= f.search_field :name_cont

I want to create a list of extra words(tokens) that will show a different query.

For example, if I type John it will show all Johns, but if I type [] it will show all users without a name.

One more: @ - will means users with a name.

New implementation also welcome.


Solution

  • In the controller, you can modify the search params according to any logic you like. E.g., assuming your search params are saved in a @search_params (replace with whatever var has your search params, e.g. params[:q]):

    if @search_params[:name_cont] == "[]"
      @search_params[:name_blank] = true
      @search_params.delete(:name_cont)
    end
    if @search_params[:name_cont] == "@"
      @search_params[:name_present] = true
      @search_params.delete(:name_cont)
    end