Search

Hi,

I’ve implemented two boxes of research which correspond to two columns
of my table. This solution works well:

In my index.html.erb:
<% form_tag search_intervenants_path, :method => :get do %>
<%= label_tag “code” %>
<%= text_field_tag “search[by_code]”,
params[:search].try(:
[], :code) %>
<%= label_tag “id” %>
<%= text_field_tag “search[by_id]”,
params[:search].try(:[], :id) %>
<%= submit_tag “Search” %>
<% end %>

In my controller:
def search
@intervenants = Intervenant.search(params[:search])
render :index
end

In my model:
def self.search(params)
params.keys.inject(scoped({})) do |found, k|
params[k].blank? ? found : found.send(:"#{k}", params[k])
end
end
scope :by_code, lambda {|name| where(“intervenants.code LIKE ?”,
“%#{name}%”)}
scope :by_id, lambda {|name| where(“intervenants.id LIKE ?”,
“%#{name}%”)}

I would like to have an only search area, I tried but I didn’t suceed
in it… Who can help me? Thanks a lot !

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs