OSDN Git Service

t#30461:fix 403 404
authoryasushiito <yas@pen-chan.jp>
Sun, 6 Jan 2013 23:51:40 +0000 (08:51 +0900)
committeryasushiito <yas@pen-chan.jp>
Sun, 6 Jan 2013 23:51:40 +0000 (08:51 +0900)
app/controllers/application_controller.rb

index 22ed9b2..7e50a98 100644 (file)
@@ -89,22 +89,37 @@ class ApplicationController < ActionController::Base
     end
   end
   
-=begin
-  rescue_from ActiveRecord::RecordNotFound, :with => :render_404
-
-  private
-  def render_404(exception = nil)
-    if exception
+  if Rails.env == 'production'
+    rescue_from ActiveRecord::RecordNotFound, :with => :render_404
+    rescue_from ActiveRecord::Forbidden, :with => :render_403
+    
+    private
+    def render_404(exception = nil)
+      if exception
         logger.info "Rendering 404: #{exception.message}"
+      end
+      respond_to do |format|
+        format.html { 
+          render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
+        }
+        format.json { 
+          render :text => "404 Not found", :status => 404
+        }
+      end
+    end
+    
+    def render_403(exception = nil)
+      if exception
+        logger.info "Rendering 403: #{exception.message}"
+      end
+      respond_to do |format|
+        format.html { 
+          render :file => "#{Rails.root}/public/403.html", :status => 404, :layout => false
+        }
+        format.json { 
+          render :text => "403 Forbidden", :status => 403
+        }
+      end
     end
-    respond_to do |format|
-          format.html { 
-    render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
-}
-          format.json { 
-    render :text => "404 found", :status => 404
-}
-  end
   end
-=end  
 end