posts_controller.rb
3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
class PostsController < ApplicationController
before_filter :current_user, except: [:sign_in]
# GET /posts
# GET /posts.json
def index
sort = []
params[:screa] ||= 'desc'
{scate: :cate, schan: :chan, screa: :created_at}.each do |key, val|
if params[key].present?
sort.push(params[key] == "desc" ? "#{val} desc" : "#{val} asc")
end
end
@posts = Post.where(isdel: false).page(params[:page]).per(20).order(sort.join(', '))
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
# GET /posts/1
# GET /posts/1.json
def show
@post = Post.find_by_flag(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @post }
end
end
# GET /posts/new
# GET /posts/new.json
def new
@post = Post.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @post }
end
end
# GET /posts/1/edit
def edit
@post = Post.find_by_flag(params[:id])
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: '文章创建成功.' }
format.json { render json: @post, status: :created, location: @post }
else
format.html { render action: "new" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PUT /posts/1
# PUT /posts/1.json
def update
@post = Post.find_by_flag(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
format.html { redirect_to @post, notice: '文章修改成功.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post = Post.find_by_flag(params[:id])
@post.update_attributes(isdel: true) if @post
redirect_to :back
end
# 上传图片
def upload
if params[:upload_file].present?
upload = params[:upload_file]
ext = File.extname(upload.original_filename).downcase
picture = Picture.create(filename: upload.original_filename)
system("convert #{upload.tempfile.path} -auto-orient -resize '1000>' -gravity center -strip -quality 85 -format jpg public/uploads/#{picture.id}.jpg")
# 有一个2:1.5的缩略图裁剪 最宽200
# File.open("public/uploads/#{picture.id}#{ext}", "wb") { |f| f.write(upload.read) }
render json: {success: true, msg: '上传成功', file_path: "/uploads/#{picture.id}.jpg"}
else
render json: {success: false, msg: '上传错误,联系管理员', file_path: '/images/logo.png'}
end
end
def sign_in
if request.post?
if params[:username] == 'normz' and params[:password] == 'normz-nuozheng'
session[:current_user] = 'normz'
redirect_to posts_path
else
flash[:notice] = '账号或密码错误'
redirect_to :sign_in
end
end
end
def sign_out
session[:current_user] = nil
redirect_to root_path
end
private
def current_user
unless session[:current_user] == 'normz'
redirect_to :sign_in and return
end
end
end