# Imitate "Unknown action"?

**URL:** <https://www.ruby-forum.com/t/imitate-unknown-action/120107>\
**Category:** Rails\
**Created:** [November 13, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/imitate-unknown-action/120107 "2007-11-13T20:21:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 13, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/imitate-unknown-action/120107/1 "2007-11-13T20:21:47Z")

</div>

Hi all

I’m integrating some basic authorization stuff. When a user hits  
/countries/some\_unknown\_action, then Rails displays a “Unknown action”  
message (in development mode). When an unauthorized user hits  
/countries/action\_that\_needs\_authorization, then I’d like Rails to  
behave exactly the same way (because some error like “you don’t have  
permission to…” would encourage hackers to re-attempt the action).

I already tried it with

return render(:file =\> “#{RAILS\_ROOT}/public/404.html”, :layout =\>  
false, :status =\> 404)

But I guess that’s not really the best way, because it’s hard-coded and  
doesn’t rely on Rails’ own mechanism to display the 404 page. So I tried  
it using

raise ActionController::UnknownAction

But it seems that this Exception is only used by the functional tests.  
So what should I do to rely on Rails’ own mechanism to display the 404  
page?

Thanks a lot  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 13, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/imitate-unknown-action/120107/2 "2007-11-13T20:30:32Z")

</div>

Oh, even cooler would be to distinguish between production and  
development mode!

In development mode I’d like to display an Exception, and in  
production/test mode it should behave like described above.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 13, 2007, 8:31pm UTC](https://www.ruby-forum.com/t/imitate-unknown-action/120107/3 "2007-11-13T20:31:57Z")

</div>

Wouldn’t anyone seeing that error just become confused as to why a  
development-mode error message was being shown during a production  
environment, though? This would encourage me to dig deeper than just a  
‘get out of my stuff’ message.

On Nov 13, 2007 7:21 PM, Joshua M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I already tried it with  
> But it seems that this Exception is only used by the functional tests.  
> So what should I do to rely on Rails’ own mechanism to display the 404  
> page?
> 
> ## Thanks a lot Josh
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Edd Morgan  
[http://www.eddm.co.uk](http://www.eddm.co.uk)  
+44 (0) 7805 089097

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 13, 2007, 8:39pm UTC](https://www.ruby-forum.com/t/imitate-unknown-action/120107/4 "2007-11-13T20:39:02Z")

</div>

Edd Morgan wrote:

> Wouldn’t anyone seeing that error just become confused as to why a  
> development-mode error message was being shown during a production  
> environment, though? This would encourage me to dig deeper than just a  
> ‘get out of my stuff’ message.

Well, maybe I confused some stuff in my post. In fact I just want to  
imitate the same behavior that Rails shows when an unknown action is  
called, independent of whether I’m in development or in production mode:

- When in development mode, do what Rails would do
- When in production mode, do what Rails would do

I then only added the useful exception (in my 2nd post) of displaying  
some more info for developers so they can distinguish between really not  
existing actions and now allowed actions.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 13, 2007, 10:10pm UTC](https://www.ruby-forum.com/t/imitate-unknown-action/120107/5 "2007-11-13T22:10:31Z")

</div>

Joshua M. wrote:

> Oh, even cooler would be to distinguish between production and  
> development mode!
> 
> In development mode I’d like to display an Exception, and in  
> production/test mode it should behave like described above.

This may help:

> **[ActionController::Rescue](https://api.rubyonrails.org/classes/ActionController/Rescue.html)**
>
> This module is responsible for providing rescue\_from to controllers, wrapping actions to handle configured errors, and configuring when detailed exceptions must be shown.

Allows you to define what should happen when a controller raises an  
exception that isn’t rescued both in development and production.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 13, 2007, 11:50pm UTC](https://www.ruby-forum.com/t/imitate-unknown-action/120107/6 "2007-11-13T23:50:10Z")

</div>

Daniel W. wrote:

> Joshua M. wrote:
> 
> > Oh, even cooler would be to distinguish between production and  
> > development mode!
> > 
> > In development mode I’d like to display an Exception, and in  
> > production/test mode it should behave like described above.
> 
> This may help:  
> [ActionController::Rescue](http://api.rubyonrails.org/classes/ActionController/Rescue.html)
> 
> Allows you to define what should happen when a controller raises an  
> exception that isn’t rescued both in development and production.

Thank you! But this is only the “counterpart” of what I want to do -  
it’s the way to catch exceptions, but I want to throw one (I guess)…

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 13, 2007, 11:52pm UTC](https://www.ruby-forum.com/t/imitate-unknown-action/120107/7 "2007-11-13T23:52:53Z")

</div>

Are you looking for raise?

raise “I blew up!”

-Bill

Joshua M. wrote:

> > This may help:  
> > [ActionController::Rescue](http://api.rubyonrails.org/classes/ActionController/Rescue.html)
> > 
> > Allows you to define what should happen when a controller raises an  
> > exception that isn’t rescued both in development and production.
> 
> Thank you! But this is only the “counterpart” of what I want to do -  
> it’s the way to catch exceptions, but I want to throw one (I guess)…

–  
Sincerely,

William P.
