diff app/controllers/pastes_controller.rb @ 32:59ef6698fa0d

Rescue from not unique error by redirecting to previously pasted paste.
author Edho Arief <edho@myconan.net>
date Mon, 10 Sep 2012 04:14:31 +0700
parents f3adcff48173
children f649b46fca4f
line wrap: on
line diff
--- a/app/controllers/pastes_controller.rb	Mon Sep 10 04:11:16 2012 +0700
+++ b/app/controllers/pastes_controller.rb	Mon Sep 10 04:14:31 2012 +0700
@@ -33,14 +33,18 @@
     @paste.paste = params[:paste][:paste]
     @paste.ip = request.remote_ip
 
-    respond_to do |format|
-      if @paste.save
-        format.html { redirect_to @paste, :notice => 'Paste was successfully created.' }
-        format.json { render :json => @paste, :status => :created, :location => @paste }
-      else
-        format.html { render :action => "new" }
-        format.json { render :json => @paste.errors, :status => :unprocessable_entity }
+    begin
+      respond_to do |format|
+        if @paste.save
+          format.html { redirect_to @paste, :notice => 'Paste was successfully created.' }
+          format.json { render :json => @paste, :status => :created, :location => @paste }
+        else
+          format.html { render :action => "new" }
+          format.json { render :json => @paste.errors, :status => :unprocessable_entity }
+        end
       end
+    rescue ActiveRecord::RecordNotUnique
+      redirect_to paste_path(Paste.where(:ip => @paste.ip, :paste_hash => @paste.paste_hash).first)
     end
   end