I'm looking for a solution that will allow my rails app to render a user-friendly maintenance page when there is no Mysql server available to connect to.
/!\ FAILSAFE /!\ Wed May 26 11:40:14 -0700 2010
Status: 500 Internal Server Error
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock
You could create a view in whatever your root_path controller is:
map.root :controller => "foo", :action => "index"
Let's say you call this view "db_maintenance.html.erb". In your controller, do this:
def index begin @widgets = Widget.find(:all) rescue Exception => e # This will only happen if DB stuff fails redirect_to :action => "db_maintenance", :error => e.message end end ... def db_maintenance @error = params[:error] # You might want to do something with this here or in the view # renders the app/views/foo/db_maintenance.html.erb view end
In your view, you could put something like:
<h1>Sorry for the inconvenience</h1> blah blah blah. This happened because of: <pre><code><%= @error %></code></pre>
This, ofcourse, only helps if the user hits your site's main page, but you could easily extrapolate from there. You could add the "def db_maintenance" action to the application controller and manually specify what view it should render too. It's not perfect, but it should get the job done.