about summary refs log tree commit diff stats
path: root/app/controllers/admin/blogs_controller.rb
blob: f0ce519531f3ea87d17cc1293a238d1d485bf60c (plain) (blame)
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
class Admin::BlogsController < Admin::AdminController
  before_action :set_section

  def index
    @blogs = Blog.where(published: true).order(published_at: :desc)
  end

  def drafts
    @blogs = Blog.where(published: false).order(updated_at: :desc)
  end

  def show
    @blog = Blog.find(params[:id])

    if @blog.published
      redirect_to @blog
    else
      render layout: "application"
    end
  end

  def new
    @blog = Blog.new
  end

  def create
    @blog = Blog.new(blog_params)
    @blog.user = current_user

    if @blog.save
      flash.notice = "Blog created successfully!"

      render :edit
    else
      flash.alert = "Error creating blog."

      render :new
    end
  end

  def edit
    @blog = Blog.find(params[:id])
  end

  def update
    @blog = Blog.find(params[:id])

    if @blog.update(blog_params)
      flash.notice = "Blog updated successfully!"
    else
      flash.alert = "Error updating blog."
    end

    render :edit
  end

  private

    def blog_params
      params.require(:blog).permit(:title, :body, :slug, :published, :published_at, :tag_list, records_attributes: [:description, :_destroy])
    end

    def set_section
      @section = "blogs"
    end

end