Rails - how not to rate a field in a Ransack search form

I am using Ransack Gem in Rails 3.1. Everything works as expected. However, I have a search form that will search for the person using the Soundex column that I am having a problem with.

In my form, I have a given_name_soundex_cont field so that the user enters the name "Joe". My controller than converts "joe" to soundex code, and then Ransack looks for a match in the "given_name_soundex" column.

All matching entries are returned and seem fine, except that the field in which the user entered the word "joe" now shows the soundex value (of course, because I changed the parameter).

So I decided that I could just add the given_name_cont field and hide the given_name_soundex_cont field, but now Ransack wants to include in the given_name_cont request, which I don’t want.

Does anyone know how I can include fields in a Ransack search form without checking Ransack them. Thus, I can indicate which fields will be evaluated and which will not.

If this helps, this is what I have in the controller:

def index

  if !params[:q].blank?  # nil.blank? and [].blank? are true
    search_params = params[:q]
    search_params[:given_name_soundex_cont] = convert_to_soundex(search_params[:given_name_soundex_cont])
    @q = Person.search(search_params)
    @results = @q.result.limit(50)
  else
    @q = Person.search(params[:q])
    @results = []
  end

end


private 
    def convert_to_soundex(text)
      Text::Soundex.soundex(text.to_s)
    end

And in my opinion:

<%= search_form_for @q do |f| %>
  <label>Given Name:</label>
  <%= f.text_field :given_name_soundex_cont %>

  <%= f.submit %>
<% end %>
+5
source share
3 answers

Create an ransacker that formats the parameter. I have not tried this, but I think it will work (via https://github.com/ernie/ransack/issues/36 ).

ransacker :given_name_soundex, :formatter => proc {|v| Text::Soundex.soundex(v)} do
  parent.table[:given_name_soundex]
end
+1
source

params[:q][:given_name_soundex] , , #ransack/#search.

0

- , SQL %, "Foo Bar" "Foo Bar", "Foomster Q. Bar" "Foo Glolubar". , "Fiefoo Newton Barrister" .

. app/controllers/customers_controller.rb:

class CustomersController < ApplicationController
  # GET /customers
  # GET /customers.json
  def index
    @search = Customer.search(params[:q])
    @customers = @search.result
    ...
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @customers }
    end
  end
  ...
end

app/views/customers/index.html.erb ( span4, span8 btn btn-large btn-primary Twitter Bootstrap framework):

<% require 'action_view' %>
<div class="row">
...
  <div class="span4"> <!-- The search criteria -->
    ...
    <%= search_form_for @search do |f| %> <!-- this is a Ransack-provided form -->
      <div class="field">
        <%= f.label :customer_name_whitespaces_match_anything, "Name is or contains:" %>
        <%= f.text_field :customer_name_whitespaces_match_anything, class: "my-textbox" %>
        ...
        <%= f.label :customer_address_whitespaces_match_anything, "Address is or contains:" %>
        <%= f.text_field :customer_address_whitespaces_match_anything, class: "my-textbox" %>
        ...
      </div>
      <br/>
      <div class="actions">
        <%= f.submit "Search", class: "btn btn-large btn-primary" %>
      </div>
    <% end %>
  </div>
  <div class="span8"> <!-- The search results -->
    ...
    <table border="1" cellpadding="5">
      <tr>
        <th><%= sort_link(@search, :customer_name, "Customer name") %></th>
        ...
        <th><%= sort_link(@search, :customer_address, "Address") %></th>
        ...
      </tr>
      <% @customers.each do |c| %>
        <tr>
          <td><%= link_to c.customer_name, customer_path(c, search: @search) %></td>
          ...
          <td><%= c.customer_address %></td>
          ...
        </tr>
      <% end %>
    </table>
  </div>
...
</div>

customer_name_whitespaces_match_anything customer_address_whitespaces_match_anything. search predicate, config/initializers/ransack.rb. :

Ransack.configure do |config|
  config.add_predicate 'whitespaces_match_anything',
  :arel_predicate => 'matches', # so we can use the SQL wildcard "%"
  # Format the incoming value: replace spaces by the SQL wildcard "%".
  :formatter => proc {|v| "%"+v.gsub(" ","%")+"%"}
end

SQL , - .

( , app/views/customers/show.html.erb .)

0

All Articles