23

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?

Ganesh Shankar
  • 4,826
  • 8
  • 43
  • 56
  • I think the accepted answer should be changed -- this one is a better solution, since it's the correct error code as well as a better presentation to the end user: http://stackoverflow.com/questions/9130191/how-to-return-correct-http-error-codes-from-ruby-on-rails-application – Stewart Johnson Apr 15 '13 at 13:19

2 Answers2

44

In your controller code add the following line:

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

Refer to this page for http code to symbol mapping.

swilliams
  • 48,060
  • 27
  • 100
  • 130
Harish Shetty
  • 64,083
  • 21
  • 152
  • 198
5

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.

Community
  • 1
  • 1
Hugo Logmans
  • 2,202
  • 1
  • 19
  • 14