changeset 229:388504e43bcf

Properly return 404 when requesting invalid paste.
author Edho Arief <edho@myconan.net>
date Wed, 15 May 2013 14:10:35 +0900
parents 4a79bc78e90e
children 1c750d3cde1b
files app/controllers/pastes_controller.rb app/models/paste.rb
diffstat 2 files changed, 6 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/app/controllers/pastes_controller.rb	Mon May 13 13:20:53 2013 +0900
+++ b/app/controllers/pastes_controller.rb	Wed May 15 14:10:35 2013 +0900
@@ -5,6 +5,7 @@
   # GET /1.txt
   def show
     @paste = Paste.safe_find(params[:id])
+    head :not_found and return unless @paste
 
     expires_in 1.year, :public => true
     respond_to do |format|
--- a/app/models/paste.rb	Mon May 13 13:20:53 2013 +0900
+++ b/app/models/paste.rb	Wed May 15 14:10:35 2013 +0900
@@ -15,7 +15,11 @@
   def self.safe_find(raw_id)
     id, secret = raw_id.split("-")
     return unless id.to_i.to_s == id
-    self.where(secret: secret).find(id)
+    begin
+      self.where(secret: secret).find(id)
+    rescue ActiveRecord::RecordNotFound
+      nil
+    end
   end
 
   def path