Class: Homeland::Press::PostsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/homeland/press/posts_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#current_ability

Instance Method Details

#createObject

POST /posts



37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/homeland/press/posts_controller.rb', line 37

def create
  authorize! :create, Post
  @post = Post.new(post_params)
  @post.user_id = current_user.id

  if @post.save
    redirect_to @post, notice: '文章提交成功,需要等待管理员审核。'
  else
    render :new
  end
end

#destroyObject

DELETE /posts/1



66
67
68
69
70
# File 'app/controllers/homeland/press/posts_controller.rb', line 66

def destroy
  authorize! :destroy, @post
  @post.destroy
  redirect_to posts_url, notice: '文章删除成功。'
end

#editObject

GET /posts/1/edit



32
33
34
# File 'app/controllers/homeland/press/posts_controller.rb', line 32

def edit
  authorize! :update, @post
end

#indexObject

GET /posts



7
8
9
# File 'app/controllers/homeland/press/posts_controller.rb', line 7

def index
  @posts = Post.includes(:user).published.order('published_at desc, id desc').page(params[:page]).per(10)
end

#newObject

GET /posts/new



26
27
28
29
# File 'app/controllers/homeland/press/posts_controller.rb', line 26

def new
  authorize! :create, Post
  @post = Post.new
end

#previewObject



20
21
22
23
# File 'app/controllers/homeland/press/posts_controller.rb', line 20

def preview
  out = Homeland::Markdown.call(params[:body])
  render plain: out
end

#publishObject



59
60
61
62
63
# File 'app/controllers/homeland/press/posts_controller.rb', line 59

def publish
  authorize! :publish, @post
  @post.published!
  redirect_to posts_path, notice: "文章审核成功。"
end

#showObject

GET /posts/1



16
17
18
# File 'app/controllers/homeland/press/posts_controller.rb', line 16

def show
  @post.hits.incr(1)
end

#upcomingObject



11
12
13
# File 'app/controllers/homeland/press/posts_controller.rb', line 11

def upcoming
  @posts = Post.includes(:user).upcoming.order('id desc').page(params[:page]).per(10)
end

#updateObject

PATCH/PUT /posts/1



50
51
52
53
54
55
56
57
# File 'app/controllers/homeland/press/posts_controller.rb', line 50

def update
  authorize! :update, @post
  if @post.update(post_params)
    redirect_to @post, notice: '文章更新成功。'
  else
    render :edit
  end
end