form_tag not routing correctly
I am trying to get a form tag working in rails 3 but I keep getting a Routing Error:
No route matches {:action=>"search", :controller=>"posts"}.
Here is the code for the for_tag:
<%= form_tag search_post_path, method: :get do %>
<br />
<fieldset>
<legend>Search</legend>
<%= text_field_tag :search, params[:search], :id => 'search_field' %>
<%= submit_tag "Search", :name => nil %>
<%= link_to_function "Clear", "$('search_field').clear()" %>
</fieldset>
<br />
<% end %>
I have a method开发者_Go百科 in my posts_controller that is called search. This is what put in my routes.rb file:
post 'search' => 'posts#search'
resources :posts do
member do
post 'search'
get 'search'
end
end
I feel like I have tried everything in my routes file and nothing worked. At one point I got the for_for to show up but then when I hit the submit button, I got a NoMethodError for the method 'search'.
You route "search" is on a member, so the matching URL is /posts/:id/search. However you do not provide an ID, hence the error. You should put the search route inside a collection block, like so :
resources :posts do
collection do
get 'search'
end
end
So the matching URL will be /posts/search.
精彩评论