Custom 404 Rails error pages
Ruby posted 9 months ago by christian
All credit for this snippet is due to Henrik Nyh.
Put this in application_controller.rb:
1 alias_method :rescue_action_locally, :rescue_action_in_public if RAILS_ENV == 'development' 2 3 def render_optional_error_file(status_code) 4 if status_code == :not_found 5 activate_authlogic 6 render_404 7 else 8 super 9 end 10 end 11 12 def render_404 13 respond_to do |type| 14 type.html { render :template => "errors/error_404", :layout => 'application', :status => 404 } 15 type.all { render :nothing => true, :status => 404 } 16 end 17 18 true # so we can do "render_404 and return" 19 end
Tracking 404 and 500 with Google Analytics
JavaScript posted about 1 year ago by christian
Tracking 404 and 500 errors with Google Analytics is documented here, but I tend to forget so I’m putting the information here:
1 // 404 2 pageTracker._trackPageview("/404.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer); 3 4 // 500 5 pageTracker._trackPageview("/500.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer);
In Rails I set the response code and use that instead of hardcoding it in the view:
1 <% if response.status != 404 %> 2 pageTracker._trackPageview(); 3 <% else %> 4 pageTracker._trackPageview("/404.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer); 5 <% end %>