Retrospring/app/controllers/announcement_controller.rb

57 lines
1.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2020-04-19 12:12:22 -07:00
class AnnouncementController < ApplicationController
before_action :authenticate_user!
2020-04-19 12:12:22 -07:00
def index
@announcements = Announcement.all
end
def new
2020-04-19 12:34:48 -07:00
@announcement = Announcement.new
2020-04-19 12:12:22 -07:00
end
def create
2020-04-19 12:34:48 -07:00
@announcement = Announcement.new(announcement_params)
@announcement.user = current_user
if @announcement.save
flash[:success] = t(".success")
2020-04-19 12:34:48 -07:00
redirect_to action: :index
else
flash[:error] = t(".error")
render "announcement/new"
2020-04-19 12:34:48 -07:00
end
2020-04-19 12:12:22 -07:00
end
def edit
2020-04-19 12:50:33 -07:00
@announcement = Announcement.find(params[:id])
2020-04-19 12:12:22 -07:00
end
def update
@announcement = Announcement.find(params[:id])
@announcement.update(announcement_params)
if @announcement.save
2022-01-24 13:17:53 -08:00
flash[:success] = t(".success")
2020-04-19 12:58:57 -07:00
redirect_to announcement_index_path
else
2022-01-24 13:17:53 -08:00
flash[:error] = t(".error")
render "announcement/edit"
2020-04-19 12:58:57 -07:00
end
2020-04-19 12:12:22 -07:00
end
def destroy
2020-04-19 12:50:33 -07:00
if Announcement.destroy(params[:id])
flash[:success] = t(".success")
2020-04-19 12:50:33 -07:00
else
flash[:error] = t(".error")
2020-04-19 12:50:33 -07:00
end
redirect_to announcement_index_path
2020-04-19 12:12:22 -07:00
end
2020-04-19 12:34:48 -07:00
private
def announcement_params
params.require(:announcement).permit(:content, :link_text, :link_href, :starts_at, :ends_at)
end
2020-04-19 12:12:22 -07:00
end