about summary refs log tree commit diff stats
path: root/app/controllers/admin/links_controller.rb
blob: 54f245a37d5a4170f0843573a42c9409fd8f21b5 (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
class Admin::LinksController < Admin::AdminController
  before_action :set_section

  def index
    @links = Link.order(created_at: :desc)
  end

  def new
    @link = Link.new
  end

  def create
    @link = Link.new(link_params)

    if @link.save
      flash.notice = "Link created successfully!"

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

      render :new
    end
  end

  def edit
    @link = Link.find(params[:id])
  end

  def update
    @link = Link.find(params[:id])

    if @link.update(link_params)
      flash.notice = "Link updated successfully!"
    else
      flash.alert = "Error updating link."
    end

    render :edit
  end

  private

    def link_params
      params.require(:link).permit(:title, :url, :tag_list, records_attributes: [:description, :_destroy])
    end

    def set_section
      @section = "links"
    end

end