Class: Blog::PostsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/blog/posts_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /blog/posts



42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/blog/posts_controller.rb', line 42

def create
  authorize! :create, @blog_post
  @blog_post = Post.new(blog_post_params)

  if @blog_post.save
    redirect_to @blog_post, notice: 'Post was successfully created.'
  else
    render :new
  end
end

#destroyObject

DELETE /blog/posts/1



63
64
65
66
# File 'app/controllers/blog/posts_controller.rb', line 63

def destroy
  @blog_post.destroy
  redirect_to blog_posts_url, notice: 'Post was successfully destroyed.'
end

#editObject

GET /blog/posts/1/edit



38
39
# File 'app/controllers/blog/posts_controller.rb', line 38

def edit
end

#indexObject

GET /blog/posts



7
8
9
10
11
12
13
# File 'app/controllers/blog/posts_controller.rb', line 7

def index
  if params[:tag]
    @blog_posts = Post.tagged_with(params[:tag]).page params[:page]
  else
    @blog_posts = Post.all.page params[:page]
  end
end

#listObject



15
16
17
18
19
20
21
22
# File 'app/controllers/blog/posts_controller.rb', line 15

def list

  if params[:tag]
    @blog_posts = Post.tagged_with(params[:tag]).page params[:page]
  else
    @blog_posts = Post.where('? >= published_at and draft = ?', DateTime.now, false).includes(:user).page params[:page]
  end
end

#newObject

GET /blog/posts/new



33
34
35
# File 'app/controllers/blog/posts_controller.rb', line 33

def new
  @blog_post = Post.new
end

#showObject

GET /blog/posts/1



25
26
# File 'app/controllers/blog/posts_controller.rb', line 25

def show
end

#show_listObject



28
29
30
# File 'app/controllers/blog/posts_controller.rb', line 28

def show_list
  @blog_post.increment!(:access_count)
end

#updateObject

PATCH/PUT /blog/posts/1



54
55
56
57
58
59
60
# File 'app/controllers/blog/posts_controller.rb', line 54

def update
  if @blog_post.update(blog_post_params)
    redirect_to @blog_post, notice: 'Post was successfully updated.'
  else
    render :edit
  end
end