Search multiple fields with multiple values

时间:2017-06-12 17:04:06

标签: ruby-on-rails ruby

So what I have is a model Referent that has multiple attributes for example nom and prenom. I was able to search each attribute in my model using one search value. But then I tried having one text_field for each attribute so for nom I would have one text_field and for prenom I would have another.

So it would search for all Referent who have that nom and that prenom but I'm not capable of seperating those two search. Right now it just take one of the value and search in both nom and prenom with the same value

View:

<h2>Search Referent</h2>
<%= form_tag(referents_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Nom" %>
<%= text_field_tag :search, params[:search], placeholder: "Prenom" %>
<%= submit_tag "Search", class: 'btn btn-info' %>
<% end %>

Controller:

def index
  @referents = Referent.all
  if params[:search]
    @referents = Referent.search(params[:search]).order("created_at DESC")
  else
    @referents = Referent.all.order("created_at DESC")
  end
end

Model:

def self.search(search)
  where("nom || prenom ILIKE ?", "%#{search}%")
end

Right now it just seems to take the value of the second text_field and use that for the search. I'm using postgresql.

The full view:

<div class="container">
<h2>Search Referent</h2>
<%= form_tag(referents_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search_nom, params[:search], placeholder: "Nom" %>
<%= text_field_tag :search_prenom, params[:search], placeholder: "Prenom" %>
<%= submit_tag "Search", class: 'btn btn-info' %>
<% end %>

<h2>List de Referent</h2>

<table class="table table-hover">
  <tr>
    <th>Nom</th>
    <th>Prenom</th>
    <th>Titre</th>
    <th>Departement</th>
    <th>Cellulaire</th>
    <th>Bureau</th>
    <th>Fax</th>
    <th>Courriel</th>
    <th>Organisme Referent</th>
  </tr>
  <% @referents.each do |referent| %>
  <tr>
    <td><%= referent.nom %></td>
    <td><%= referent.prenom %></td>
    <td><%= referent.titre %></td>
    <td><%= referent.departement %></td>
    <td><%= referent.cellulaire %></td>
    <td><%= referent.bureau %></td>
    <td><%= referent.fax %></td>
    <td><%= referent.courriel %></td>
    <td><%= link_to referent.organismereferent.nom_organisation, organismereferent_path(referent.organismereferent_id) %></td>
  </tr>
  <% end %>
</table>

</div>

Error using Ramon answerenter image description here

1 个答案:

答案 0 :(得分:4)

我会这样做

<h2>Search Referent</h2>
<%= form_tag(referents_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search_nom, params[:search_nom], placeholder: "Nom" %>
<%= text_field_tag :search_prenom, params[:search_prenom], placeholder: "Prenom" %>
<%= submit_tag "Search", class: 'btn btn-info' %>
<% end %>

控制器

def index
  @referents = Referent.all

  search_nom = params[:search_nom]
  search_prenom = params[:search_prenom]

  @referents = Referent.search(search_nom, search_prenom).order("created_at DESC")
end

模型

def self.search(search_nom, search_prenom)
  where("nom ILIKE ? or prenom ILIKE ?", "%#{search_nom}%", "%#{search_prenom}%")
end