Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger a HTTP error in Rails

I'm trying to call a custom instance of a 403 HTTP error in Rails but I can't seem to figure out how to do this...

I have several user authentication roles and basically if a role tries to browse to an area that it is not authorised to visit I want to display a 403 instead of just redirecting the user.

How do I do this?

like image 818
Ganesh Shankar Avatar asked Mar 07 '10 03:03

Ganesh Shankar


2 Answers

In your controller code add the following line:

render :status => :forbidden, :text => "Forbidden fruit"

Refer to this page for http code to symbol mapping.

like image 138
Harish Shetty Avatar answered Oct 15 '22 11:10

Harish Shetty


Just for posterity: I think return 403.html is a nicer solution, because it generates a standard page, just like the 404. The above solution only displays the text given. And because someone in good practice can only go to a forbidden page by typing or clicking a link outside the web app, a standard error page is the way to go.

like image 32
Hugo Logmans Avatar answered Oct 15 '22 10:10

Hugo Logmans