Search code examples
ruby-on-railsformssearchmodels

Ruby on Rails - Search on Model Index


I want to be able to search and update the index.

this is my controller method:

def index
    if params[:search]
      @ofertas = Oferta.search(params[:search]).paginate(page: params[:ofertas_page], :per_page => 5)
    else
      @ofertas = Oferta.all.paginate(page: params[:ofertas_page], :per_page => 5)
    end
  end

My search method in the model

def self.search(search)
    where("titulo like ?","%w{search}%")
  end

and this is the search form

<%= form_tag ofertas_path, :method => 'get' do %>
  <p>
    <%= text_field_tag :search, params[:search], placeholder: "Procurar Entidades" %>
    <%= submit_tag "Procurar", :name => nil %>
  </p>
<% end %>

I've seen this setup in a number of search tutorials but no matter what I type nothing appears. Does someone know what I'm doing wrong?


Solution

  • It looks like you were trying to interpolate the search variable into the string, but didn't quite get the right symbol. How about this:

    "%#{search}%"
    

    Note the # instead of the w.