Rails: Trying to add search function - keep getting “no route matches”
Clash Royale CLAN TAG#URR8PPP
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?
routes.rb
routes.rb
I tried adding get ‘animals/search’, to: ‘animals#search’, but that isn't working either.
– Leia_Organa
Aug 7 at 23:26
You should really add your
routes.rb
to your question. And, run rake routes
in your console to see what you have defined.– jvillian
Aug 7 at 23:31
routes.rb
rake routes
1 Answer
1
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
GET /search
config/route.rb
resources :animals do
get :search
end
or
get search: "animals#search"
Don't assume that OP hasn't defined a route for the request unless we know how the routes.rb looks.
– Jagdeep Singh
Aug 8 at 7:19
Yes. You are right. I updated my answer. Thanks. I don't have enough reputation to clarify posts yet. So I based it on the given guide and error message.
– neume
Aug 8 at 8:24
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Did you define the route in your
routes.rb
file? You should probably add theroutes.rb
file content to your question.– jvillian
Aug 7 at 23:05