# HG changeset patch # User Edho Arief # Date 1347225271 -25200 # Node ID 59ef6698fa0dc3708e01ebca699ecd4f4221d5a6 # Parent 81c86eb273f46b72f334453c781bc1154a80527c Rescue from not unique error by redirecting to previously pasted paste. diff -r 81c86eb273f4 -r 59ef6698fa0d app/controllers/pastes_controller.rb --- 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