# Default routes for unknown actions

**URL:** <https://www.ruby-forum.com/t/default-routes-for-unknown-actions/50561>\
**Category:** Rails\
**Created:** [December 18, 2005, 4:12am UTC](https://www.ruby-forum.com/t/default-routes-for-unknown-actions/50561 "2005-12-18T04:12:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [December 18, 2005, 4:12am UTC](https://www.ruby-forum.com/t/default-routes-for-unknown-actions/50561/1 "2005-12-18T04:12:07Z")

</div>

Hi folks-

I have the following map for default ‘junk’ routes:

map.connect ‘\*anything’, :controller =\> ‘welcome’, :action =\>  
‘unknown’

Which works just fine for a URL like: “[mysite.com/junkjunkjunk](http://mysite.com/junkjunkjunk)”

However, it still tries to resolve an action when I do:  
“[mysite.com/my\_controller/junkjunkjunk](http://mysite.com/my_controller/junkjunkjunk)”

How can I send Rails to a default route in the case of an unknown  
action? Currently, I get the standard “Unknown action… No action  
responded to junkjunkjunk”

Jake

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [December 20, 2005, 8:08am UTC](https://www.ruby-forum.com/t/default-routes-for-unknown-actions/50561/2 "2005-12-20T08:08:27Z")

</div>

Jake J. wrote:

> I have the following map for default ‘junk’ routes:
> 
> map.connect ‘\*anything’, :controller =\> ‘welcome’, :action =\>  
> ‘unknown’
> 
> Which works just fine for a URL like: “[mysite.com/junkjunkjunk](http://mysite.com/junkjunkjunk)”
> 
> However, it still tries to resolve an action when I do:  
> “[mysite.com/my\_controller/junkjunkjunk](http://mysite.com/my_controller/junkjunkjunk)”

Please show the complete routes setup, so we can help you.

> How can I send Rails to a default route in the case of an unknown  
> action? Currently, I get the standard “Unknown action… No action  
> responded to junkjunkjunk”

It should work if you put the following action in your application  
controller. Please read the documentation for rescue\_action\_in\_public  
before returning with questions (because this method is not invoked  
for local clients).

def rescue\_action\_in\_public(exception)  
case exception  
when ActionController::UnknownAction  
render :file =\> “#{RAILS\_ROOT}/public/404.html”, :status =\> 404  
else  
logger.error "Error raised: " + exception.to\_s  
render :file =\> “#{RAILS\_ROOT}/public/500.html”, :status =\> 500  
end  
end

I actually also redirect to 404 when a ActiveRecord::RecordNotFound  
exception is encountered.

Regards,  
Patrice

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [December 20, 2005, 4:39pm UTC](https://www.ruby-forum.com/t/default-routes-for-unknown-actions/50561/3 "2005-12-20T16:39:34Z")

</div>

> def rescue\_action\_in\_public(exception)  
> case exception  
> when ActionController::UnknownAction  
> render :file =\> “#{RAILS\_ROOT}/public/404.html”, :status =\> 404  
> else  
> logger.error "Error raised: " + exception.to\_s  
> render :file =\> “#{RAILS\_ROOT}/public/500.html”, :status =\> 500  
> end  
> end

Ah. That explains a lot. Thanks. I didn’t even know this function  
existed. The hard part is rarely the search. It’s knowing what to  
search for.

Jake
