Pages 404 et 500 personnalisées dans Rails
Dans Rails 3, les pages d'erreur sont dans le répertoire public/
, on n'a donc pas
accès aux helpers de Rails, ni au layout.
Pour y remédier, on peut faire des rescue_from
dans l'application.
Dans votre application_controller.rb
:
<span class="k">unless</span> <span class="no">Rails</span><span class="o">.</span><span class="n">application</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">consider_all_requests_local</span> <span class="n">rescue_from</span> <span class="no">Exception</span><span class="p">,</span> <span class="ss">with</span><span class="p">:</span> <span class="o">-></span><span class="p">(</span><span class="n">exception</span><span class="p">)</span> <span class="p">{</span> <span class="n">render_error</span> <span class="mi">500</span><span class="p">,</span> <span class="n">exception</span> <span class="p">}</span> <span class="n">rescue_from</span> <span class="ss">ActionController</span><span class="p">:</span><span class="ss">:RoutingError</span><span class="p">,</span> <span class="ss">ActionController</span><span class="p">:</span><span class="ss">:UnknownController</span><span class="p">,</span> <span class="o">::</span><span class="ss">AbstractController</span><span class="p">:</span><span class="ss">:ActionNotFound</span><span class="p">,</span> <span class="ss">Mongoid</span><span class="p">:</span><span class="ss">:Errors</span><span class="o">::</span><span class="no">DocumentNotFound</span><span class="p">,</span> <span class="ss">with</span><span class="p">:</span> <span class="o">-></span><span class="p">(</span><span class="n">exception</span><span class="p">)</span> <span class="p">{</span> <span class="n">render_error</span> <span class="mi">404</span><span class="p">,</span> <span class="n">exception</span> <span class="p">}</span> <span class="k">end</span> <span class="k">def</span> <span class="nf">render_error</span><span class="p">(</span><span class="n">status</span><span class="p">,</span> <span class="n">exception</span><span class="p">)</span> <span class="c1"># Add this only if airbrake is used in your application</span> <span class="n">notify_airbrake</span><span class="p">(</span><span class="n">exception</span><span class="p">)</span> <span class="n">respond_to</span> <span class="k">do</span> <span class="o">|</span><span class="n">type</span><span class="o">|</span> <span class="n">type</span><span class="o">.</span><span class="n">html</span> <span class="p">{</span> <span class="n">render</span> <span class="ss">template</span><span class="p">:</span> <span class="s2">"errors/</span><span class="si">#{</span><span class="n">status</span><span class="si">}</span><span class="s2">"</span><span class="p">,</span> <span class="ss">layout</span><span class="p">:</span> <span class="s1">'application'</span><span class="p">,</span> <span class="ss">status</span><span class="p">:</span> <span class="n">status</span> <span class="p">}</span> <span class="n">type</span><span class="o">.</span><span class="n">all</span> <span class="p">{</span> <span class="n">render</span> <span class="ss">nothing</span><span class="p">:</span> <span class="kp">true</span><span class="p">,</span> <span class="ss">status</span><span class="p">:</span> <span class="n">status</span> <span class="p">}</span> <span class="k">end</span> <span class="k">end</span>
Si vous utilisez active_record
remplacer Mongoid::Errors::DocumentNotFound
par ActiveRecord::RecordNotFound
.
Il ne vous reste plus qu'a créer les templates
errors/{404,500}.html.haml
.