hey
hey

Reputation: 55

Simple search is not working in rails?

I set up a search mechanism for my Procedure table following this railscast http://railscasts.com/episodes/37-simple-search-form?autoplay=true. I pretty much followed it exactly but I get this error when I submit the search:

ActiveRecord::RecordNotFound at /procedures

Couldn't find all Procedures with 'id': (all, {:conditions=>["name LIKE ?", "%Emergency%"]}) (found 0 results, but was looking for 2)

I want to search for Procedures by name, and this error makes it look like it is searching by id?

Here is the view:

      <%= form_tag procedures_path, :method => 'get' do %>
      <p>
      <%= text_field_tag :search, params[:search], :id => "welcome-search" %>
      <%= submit_tag "Search Procedures", :name => nil, :class => "btn btn-success btn-lg" %>

My controller:

def index
  @procedures = Procedure.search(params[:search])
  render `procedures/index`
end

My model:

def self.search(search)
  if search
  find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
  find(:all)
end
end

I'm pretty confused since the railscast makes it look so easy to get working..Thanks.

Upvotes: 0

Views: 503

Answers (1)

BroiSatse
BroiSatse

Reputation: 44675

find(:all) is very old and is no longer available since rails 3.2. Instead use where:

def self.search(search)
  if search
    where('name LIKE ?', "%#{search}%")
  else
    all
  end
end

Upvotes: 2

Related Questions