comparison app/controllers/pastes_controller.rb @ 373:6e3e1e7b0212

Handle unique error in model instead of controller.
author nanaya <me@myconan.net>
date Sat, 06 Jun 2015 22:12:56 +0900
parents 26545fe719ca
children e84276bf344a
comparison
equal deleted inserted replaced
372:117accbc72c3 373:6e3e1e7b0212
26 26
27 # POST / 27 # POST /
28 # POST /pastes.json 28 # POST /pastes.json
29 # POST /pastes.txt 29 # POST /pastes.txt
30 def create 30 def create
31 @paste = Paste.new paste_params.merge(:ip => request.remote_ip) 31 created, @paste, @fresh = Paste.graceful_create paste_params.merge(:ip => request.remote_ip)
32 32
33 begin 33 respond_to do |format|
34 respond_to do |format| 34 if created
35 if @paste.save 35 if @fresh
36 @fresh = true
37 format.html { redirect_to @paste, :notice => "Paste was successfully created." } 36 format.html { redirect_to @paste, :notice => "Paste was successfully created." }
38 format.json { render :json => @paste, :status => :created, :location => @paste } 37 format.json { render :json => @paste, :status => :created, :location => @paste }
39 else 38 else
40 flash.now[:alert] = @paste.errors.full_messages.to_sentence 39 format.html { redirect_to paste_path(@paste) }
41 format.html { render :action => "new" } 40 format.json { render :json => @paste }
42 format.json { render :json => @paste.errors, :status => :unprocessable_entity }
43 end 41 end
44 format.txt 42 else
43 flash.now[:alert] = @paste.errors.full_messages.to_sentence
44 format.html { render :action => "new" }
45 format.json { render :json => @paste.errors, :status => :unprocessable_entity }
45 end 46 end
46 rescue ActiveRecord::RecordNotUnique 47 format.txt
47 @paste = Paste.where(:ip => @paste.ip, :paste_hash => @paste.paste_hash).first
48 respond_to do |format|
49 format.html { redirect_to paste_path(@paste) }
50 format.json { render :json => @paste }
51 format.txt
52 end
53 end 48 end
54 end 49 end
55 50
56 def destroy 51 def destroy
57 @paste = Paste.safe_find(params[:id]) 52 @paste = Paste.safe_find(params[:id])