# If controller doesn't exist: search database for its name

**URL:** <https://www.ruby-forum.com/t/if-controller-doesnt-exist-search-database-for-its-name/188434>\
**Category:** Rails\
**Created:** [May 11, 2010, 10:05pm UTC](https://www.ruby-forum.com/t/if-controller-doesnt-exist-search-database-for-its-name/188434 "2010-05-11T22:05:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@sso](https://www.ruby-forum.com/u/sso)\
**Post date:** [May 11, 2010, 10:05pm UTC](https://www.ruby-forum.com/t/if-controller-doesnt-exist-search-database-for-its-name/188434/1 "2010-05-11T22:05:23Z")

</div>

I’ll use my recipe example again.  
It would be nice if I could have my routes (I think) set up so that if  
somebody enters a a controller that doesn’t exist a database search  
runs and looks for the controller name as one of the categories (for  
example the user enters [http://www.com/italian](http://www.com/italian)). This is an existing  
category in the database so it would likely then redirect to  
[www.com/categories/italian](http://www.com/categories/italian) (and from there the categories controller  
would handle things).

Is this possible without violating any “rules”?

---

<div class="post-metadata">

**Author:** ![sso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@sso](https://www.ruby-forum.com/u/sso)\
**Post date:** [May 11, 2010, 10:15pm UTC](https://www.ruby-forum.com/t/if-controller-doesnt-exist-search-database-for-its-name/188434/2 "2010-05-11T22:15:55Z")

</div>

On 11 May 2010 21:03, sso [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> if somebody enters a a controller that doesn’t exist a database search  
> runs and looks for the controller name
> 
> Is this possible without violating any “rules”?

You would probably be able to do something by catching 404 errors in  
your application controller and then punting them off to a method that  
tries to guess what the user meant. Google for “Rails 404” for a few  
methods to try.

---

<div class="post-metadata">

**Author:** ![sso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@sso](https://www.ruby-forum.com/u/sso)\
**Post date:** [May 12, 2010, 5:33am UTC](https://www.ruby-forum.com/t/if-controller-doesnt-exist-search-database-for-its-name/188434/3 "2010-05-12T05:33:45Z")

</div>

Michael P. wrote:

> On 11 May 2010 21:03, sso [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > if somebody enters a a controller that doesn’t exist a database search  
> > runs and looks for the controller name
> > 
> > Is this possible without violating any “rules”?
> 
> You would probably be able to do something by catching 404 errors in  
> your application controller and then punting them off to a method that  
> tries to guess what the user meant. Google for “Rails 404” for a few  
> methods to try.

Actually, no 404 catching is necessary. All you need is a catchall  
route that sends ‘/:category’ off to a search action.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@sso](https://www.ruby-forum.com/u/sso)\
**Post date:** [May 12, 2010, 4:10pm UTC](https://www.ruby-forum.com/t/if-controller-doesnt-exist-search-database-for-its-name/188434/4 "2010-05-12T16:10:01Z")

</div>

On Tue, May 11, 2010 at 3:03 PM, sso [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

You can override method\_missing. We used that approach at my last gig  
to  
grab userr-created content from a SharePoint site.

HTH,  
Bill
