Topic: Simple Search doesn't filter records.

I was able to get a simple search form (Railscast #37) in my app to display, but when I submit the search it just displays the same set of records, without any filtering.

I am guessing I have something off between my model and controller, but being new to this I am not sure what.

Below is my controller, model, and view:

Controller:

  def index
       @products = Product.search(params[:search], params[:page])
   
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @products }
    end
  end


Model:

class Product < ActiveRecord::Base
  attr_protected :id
 
  belongs_to :customer
  belongs_to :vendor
  belongs_to :category

  def self.search(search, page)
      if search
          find(:all, :conditions => ['name LIKE ?', "%#{search}%}"])
      else
          find(:all)
      end
      paginate :per_page => 20, :page => page, order: 'date_invoiced desc'
  end
end


View:

<h1>Product Listing</h1>

<%= form_tag products_path, :method => :get do %>
    <p>
        <%= text_field_tag :search, params[:search] %>
        <%= submit_tag "Search", :name => nil%>
    </p>
<% end %>

<%= link_to 'Add Product', new_product_path %>

<p><%= will_paginate @products %></p>

<table>
  <tr>
    <th>Name</th>
    <th>Serial number</th>
    <th>Date invoiced</th>
    <th>Vendor invoice</th>
    <th>Scc invoice</th>
    <th>Customer</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @products.each do |product| %>
  <tr>
    <td><%= product.name %></td>
    <td><%= product.serial_number %></td>
    <td><%= product.date_invoiced %></td>
    <td><%= product.vendor_invoice %></td>
    <td><%= product.scc_invoice %></td>
    <td><%= product.customer_id %></td>
    <td><%= link_to 'Show', product %></td>
    <td><%= link_to 'Edit', edit_product_path(product) %></td>
    <td><%= link_to 'Destroy', product, confirm: 'Are you sure?', method: :delete %></td>
  </tr>
<% end %>
</table>

<p><%= will_paginate @products %></p>

Re: Simple Search doesn't filter records.

why don't you use debugger to check the return value of search method in your Model ?

I mean tracing wink

Re: Simple Search doesn't filter records.

Think I figured it out. Looks like an errant curly bracket