Rails: Display Maintenance Page if No Database Connection Available

ε祈祈猫儿з 提交于 2019-12-04 18:45:57

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.

I think this is about your front-end configuration. For example, if you have Apache in front of some mongrels, you can configure Apache through ErrorDocument instructions to show a suitable file in case of error.

What is your front-end?

Stephan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!