Leia_Organa
Leia_Organa

Reputation: 2094

Rails: Trying to add search function - keep getting "no route matches"

I'm trying to implement a simple search function for my Rails app by following this guide. I am trying to allow users to search for animal by name.

Here's my current code:

controllers/animals_controller.rb

class AnimalsController < ApplicationController
  def search
    @animal = Animal.search(params[:search])
  end    
end 

Here's my views/animals/index.html.erb

<%= form_tag :controller => 'animals', :action => 'search', :method => 'get' do %>
  <%= text_field_tag :search, params[:search], :id => 'search_field' %>
  <%= submit_tag "Search", :name => nil %> 
<% end %> 

And here is the error I keep getting:

No route matches {:action=>"search", :controller=>"animals", :method=>"get"}

I don't understand why this isn't working. I have the search function defined in the animal_controller.rb. Can anyone point out why the search function may not be working?

Upvotes: 0

Views: 96

Answers (1)

neume
neume

Reputation: 186

Based on the guide you are following, creating a GET /search route was never mentioned. You can define the route in config/route.rb with this

resources :animals do
  get :search
end

or

get search: "animals#search"

Upvotes: 1

Related Questions