Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to render rails static 404, 500 error pages without site layout?

I'm using rails 3. In production rails nicely handles exceptions and loads my static 404.html, 500.html etc files from my public directory. However, it loads these files into my layouts/application.html.erb file. I am looking for a way to instruct rails to load these files WITHOUT using my application layout - e.g. just serve the static html file and nothing else. What is the best way to accomplish this?

thanks!

like image 944
istan Avatar asked Mar 21 '12 15:03

istan


2 Answers

render :file => 'public/404.html', :status => :not_found, :layout => false

like image 83
mikdiet Avatar answered Sep 22 '22 01:09

mikdiet


For an advanced approach working within the Rails framework. Update your routes file:

get "/404", to: "errors#error_404"
get "/500", to: "errors#error_500"

Add an ErrorsController with:

layout false

def error_404
  render status: 404
end

def error_500
  render status: 500
end

Then within the app/views/errors/ add your error_404.erb and error_500.erb files along with a snazy image and a search bar.

More info here.

like image 26
Shadoath Avatar answered Sep 25 '22 01:09

Shadoath