简体   繁体   中英

Ransack search for integer and string values from single search field

I am using Rails 4 with Ransack.

I cant make to work searching for integer and string values.

So far I tried this code.

In view:

<%= search_form_for @search do |f| %>
  <div class="field">
    <%= f.label :name_cont, "Nepieciešama meklēšāna pēc ID, vārda un telefona nr" %>         
    <%= f.text_field :name_or_phone_number_cont_or_id_eq, :placeholder => 'Meklēt pēc ID,vārda vai tel.nr.' %>
  </div>
<div class="actions"><%= f.submit "Search" %></div>
<% end %>

In controller :

 def index

    @search = Advertisement.search(params[:q])
    @advertisements = @search.result

   respond_with(@advertisements)
  end

This gives me error:

undefined method `name_or_phone_number_cont_or_id_eq' for

Is there any workaround to this problem? Or I need to convert id to string for search purposes ?

Thanks in advance.

you need to add the code to your model, for you to be able to search for string and integer.

ransacker :id do
    Arel.sql("to_char(\"#{table_name}\".\"folio\", '99999999')")
  end

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