views:

1004

answers:

1

I'd like to 'fake' a 404 page in rails.

In PHP, I would just send a header with the error code as such:

header("HTTP/1.0 404 Not Found");

How is that done with rails?

Thank you.

+12  A: 

To return a 404 header, just use the :status option for the render method.

def action
  # here the code

  render :status => 404
end

If you want to render the standard 404 page you can extract the feature in a method.

def render_404(exception = nil)
  if exception
    logger.info "Rendering 404 with exception: #{exception.message}"
  end

  respond_to do |format|
    format.html { render :file => "#{Rails.root}/public/404.html", :status => :not_found }
    format.xml  { head :not_found }
    format.any  { head :not_found }
  end
end

and call it in your action

def action
  # here the code

  render_404
end
Simone Carletti
whoa cool! thank you very much!
yuval