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

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


Solution 1:

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

Solution 2:

You can also

raise ActiveRecord::RecordNotFound

exception.

Solution 3:

The following way was the best for me:

raise ActionController::RoutingError.new('Not Found')

or just

raise ActionController::RoutingError, 'Not Found'

Or there are some other solutions: How to redirect to a 404 in Rails?

Solution 4:

Reference:

render :file => '/path/to/some/filenotfound.rhtml', 
                status => 404, :layout => true

Solution 5:

In the ApplicationController define a method like:

def render_404
  render :file => "#{RAILS_ROOT}/public/404.html",  :status => 404
end