Mercurial > zeropaste
annotate app/controllers/pastes_controller.rb @ 73:f480cdf5f3df
Misc whitespace.
author | Edho Arief <edho@myconan.net> |
---|---|
date | Thu, 04 Oct 2012 10:58:57 +0700 |
parents | be561c3967ee |
children | 48598fc65c20 |
rev | line source |
---|---|
2 | 1 class PastesController < ApplicationController |
48
f649b46fca4f
Cache all the things! Or just the show page.
Edho Arief <edho@myconan.net>
parents:
32
diff
changeset
|
2 caches_page :show |
73 | 3 |
2 | 4 # GET /pastes/1 |
5 # GET /pastes/1.json | |
6 def show | |
53 | 7 id = params[:id].to_i |
8 if id.to_s != params[:id] | |
9 redirect_to paste_path(id) | |
10 return | |
11 end | |
2 | 12 @paste = Paste.find(params[:id]) |
13 | |
14 respond_to do |format| | |
15 format.html # show.html.erb | |
7 | 16 format.txt # show.html.erb |
2 | 17 end |
18 end | |
19 | |
20 # GET /pastes/new | |
21 # GET /pastes/new.json | |
22 def new | |
23 @paste = Paste.new | |
10 | 24 begin |
25 @paste.paste = Paste.find(params[:base]).paste | |
26 rescue | |
27 end | |
2 | 28 |
29 respond_to do |format| | |
30 format.html # new.html.erb | |
31 format.json { render :json => @paste } | |
32 end | |
33 end | |
34 | |
35 # POST /pastes | |
36 # POST /pastes.json | |
37 def create | |
38 @paste = Paste.new | |
69
be561c3967ee
Safety net to ignore invalid post requests.
Edho Arief <edho@myconan.net>
parents:
67
diff
changeset
|
39 if params[:paste] and not params[:paste][:paste].blank? |
be561c3967ee
Safety net to ignore invalid post requests.
Edho Arief <edho@myconan.net>
parents:
67
diff
changeset
|
40 @paste.paste = params[:paste][:paste] |
be561c3967ee
Safety net to ignore invalid post requests.
Edho Arief <edho@myconan.net>
parents:
67
diff
changeset
|
41 @paste.ip = request.remote_ip |
be561c3967ee
Safety net to ignore invalid post requests.
Edho Arief <edho@myconan.net>
parents:
67
diff
changeset
|
42 end |
2 | 43 |
32
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
44 begin |
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
45 respond_to do |format| |
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
46 if @paste.save |
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
47 format.html { redirect_to @paste, :notice => 'Paste was successfully created.' } |
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
48 format.json { render :json => @paste, :status => :created, :location => @paste } |
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
49 else |
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
50 format.html { render :action => "new" } |
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
51 format.json { render :json => @paste.errors, :status => :unprocessable_entity } |
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
52 end |
67
f7b7a39fb48c
Allow post in txt format pt. 2 (view and controller).
Edho Arief <edho@myconan.net>
parents:
53
diff
changeset
|
53 format.txt |
2 | 54 end |
32
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
55 rescue ActiveRecord::RecordNotUnique |
59ef6698fa0d
Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents:
12
diff
changeset
|
56 redirect_to paste_path(Paste.where(:ip => @paste.ip, :paste_hash => @paste.paste_hash).first) |
2 | 57 end |
58 end | |
59 | |
60 end |