# Redirecting to a different controller from a search box

**URL:** <https://www.ruby-forum.com/t/redirecting-to-a-different-controller-from-a-search-box/153137>\
**Category:** Rails\
**Created:** [November 12, 2008, 3:35pm UTC](https://www.ruby-forum.com/t/redirecting-to-a-different-controller-from-a-search-box/153137 "2008-11-12T15:35:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vahagn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vahagn](https://www.ruby-forum.com/u/vahagn)\
**Post date:** [November 12, 2008, 3:35pm UTC](https://www.ruby-forum.com/t/redirecting-to-a-different-controller-from-a-search-box/153137/1 "2008-11-12T15:35:56Z")

</div>

Hi,-

I have a main page with a search box. When the user enters a query and  
hits “Search” I want to redirect him to another page (another  
controller with a search action), and display the results on that  
page. My form looks like this:

```
<% form_tag({:controller => "projects", :action =>

```

“search”}, :method =\> “get”) do %\>  
Enter search phrase:   
\<%= text\_field\_tag “q”, params[:q] %\>  
  
\<% end %\>

The error I get is:

ActiveRecord::RecordNotFound in ProjectsController#show

Couldn’t find Project with ID=search

How do I override the default route for this, so it doesn’t think  
search is an id?

[http://localhost:3000/projects/search?q=foo](http://localhost:3000/projects/search?q=foo) ?

Thanks.

---

<div class="post-metadata">

**Author:** ![vahagn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vahagn](https://www.ruby-forum.com/u/vahagn)\
**Post date:** [November 12, 2008, 4:11pm UTC](https://www.ruby-forum.com/t/redirecting-to-a-different-controller-from-a-search-box/153137/2 "2008-11-12T16:11:32Z")

</div>

> How do I override the default route for this, so it doesn’t think  
> search is an id?

I can think of a few solutions to this. One way would be to add a named  
route above your resource route:  
map.projects\_search ‘projects/search’, :controller =\> ‘project\_search’,  
:action =\> ‘search’

Another possible way would be to use conditions on your existing route  
mapping, but I’m not expert enough in routing to tell you how to do  
that.

That being said, do you really need your search action to be moved out  
to a different controller? Maybe you do, and that’s fine, but I wanted  
to get you to think about whether it’s really necessary. Instead you  
could just add a custom action to your existing projects controller:

map.resources :projects, :collection =\> { :search =\> :get }

---

<div class="post-metadata">

**Author:** ![vahagn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vahagn](https://www.ruby-forum.com/u/vahagn)\
**Post date:** [November 12, 2008, 4:11pm UTC](https://www.ruby-forum.com/t/redirecting-to-a-different-controller-from-a-search-box/153137/3 "2008-11-12T16:11:38Z")

</div>

Vahagn H. wrote:

> Hi,-
> 
> I have a main page with a search box. When the user enters a query and  
> hits “Search” I want to redirect him to another page (another  
> controller with a search action), and display the results on that  
> page. My form looks like this:
> 
> ```
> <% form_tag({:controller => "projects", :action =>
> 
> ```
> 
> “search”}, :method =\> “get”) do %\>  
> Enter search phrase:   
> \<%= text\_field\_tag “q”, params[:q] %\>  
>   
> \<% end %\>
> 
> The error I get is:
> 
> ActiveRecord::RecordNotFound in ProjectsController#show
> 
> Couldn’t find Project with ID=search
> 
> How do I override the default route for this, so it doesn’t think  
> search is an id?
> 
> [http://localhost:3000/projects/search?q=foo](http://localhost:3000/projects/search?q=foo) ?
> 
> Thanks.

you are useing get method  
so the action name will passed as params[:id]  
just remove get method  
then try
