changeset 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 81c86eb273f4
children 58c6d46c3e97
files app/controllers/pastes_controller.rb
diffstat 1 files changed, 11 insertions(+), 7 deletions(-) [+]
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