Reputation: 2038
Did some searching and found lots of similar questions, but nothing quite answering what I'm looking for. Very new to Rails and have a resource:
resources :articles do
collection do
get 'search/:query' => "articles#search"
end
end
I read through the Rails API on link_to and constructed this as my best guess on how to create a link to the route "search/:query" --
<%= link_to "Search for recipes", search_articles_path(:query => "waffles") %>
but that creates a link to /articles/search?query=waffles, not to /articles/search/waffles as I'd hoped. Is there a simple way to modify this so it will work this way? I'm also considering the possibility that this is difficult because it's not very "Railsy" and I should be looking into other ways to map this.
Edit: I'm fairly sure I could get this working by using match instead of resources, but suspect that using resources would be more idiomatic and easier to manage.
Upvotes: 1
Views: 1371
Reputation: 6821
Similar to Hitesh's answer:
resources :articles do
get 'search/:query' => "articles#search", :on => collection, :as => :search
end
But to construct the link_to helper do this:
<%= link_to "Search for recipes", search_articles_path('waffles') %>
Tried this on a demo app and it worked fine. The link looked like:
http://localhost:3000/articles/search/waffles
Upvotes: 2
Reputation: 825
resources :articles do
collection do
get 'search/:query' => "articles#search",:as=>search_articles_path
end
end
use above if it is not work then use below
<%= link_to "Search for recipes", "/search/waffles") %>
Upvotes: 1