comparison 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
comparison
equal deleted inserted replaced
31:81c86eb273f4 32:59ef6698fa0d
31 def create 31 def create
32 @paste = Paste.new 32 @paste = Paste.new
33 @paste.paste = params[:paste][:paste] 33 @paste.paste = params[:paste][:paste]
34 @paste.ip = request.remote_ip 34 @paste.ip = request.remote_ip
35 35
36 respond_to do |format| 36 begin
37 if @paste.save 37 respond_to do |format|
38 format.html { redirect_to @paste, :notice => 'Paste was successfully created.' } 38 if @paste.save
39 format.json { render :json => @paste, :status => :created, :location => @paste } 39 format.html { redirect_to @paste, :notice => 'Paste was successfully created.' }
40 else 40 format.json { render :json => @paste, :status => :created, :location => @paste }
41 format.html { render :action => "new" } 41 else
42 format.json { render :json => @paste.errors, :status => :unprocessable_entity } 42 format.html { render :action => "new" }
43 format.json { render :json => @paste.errors, :status => :unprocessable_entity }
44 end
43 end 45 end
46 rescue ActiveRecord::RecordNotUnique
47 redirect_to paste_path(Paste.where(:ip => @paste.ip, :paste_hash => @paste.paste_hash).first)
44 end 48 end
45 end 49 end
46 50
47 end 51 end