Alex Rivera | Logout

How do you issue a 404 response from a rails controller action?

Asked 2009-04-23T00:13:55.323
48

What's the preferred way to issue a 404 response from a rails controller action?

Edit
Report

3 Answers

39

This seems good...

# Rails 2 and below
render :file => "#{RAILS_ROOT}/public/404.html",  :status => 404

# Rails 3 and up
render :file => "#{Rails.root}/public/404.html",  :status => 404
answered 2009-04-23T00:35:47.877
39

You can also

raise ActiveRecord::RecordNotFound

exception.

answered 2011-02-01T07:42:33.757
7

In the ApplicationController define a method like:

def render_404
  render status: 404, layout: false file: Rails.root.join("public/404.html")
end
answered 2009-04-24T07:33:01.020

Your Answer