2020-04-19 12:12:22 -07:00
|
|
|
class AnnouncementController < ApplicationController
|
2020-04-19 13:45:07 -07:00
|
|
|
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] = "Announcement created successfully."
|
|
|
|
redirect_to action: :index
|
|
|
|
else
|
|
|
|
render 'announcement/new'
|
|
|
|
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
|
2020-04-19 13:26:55 -07:00
|
|
|
@announcement = Announcement.find(params[:id])
|
|
|
|
@announcement.update(announcement_params)
|
|
|
|
if @announcement.save
|
2020-04-19 12:58:57 -07:00
|
|
|
flash[:success] = "Announcement updated successfully."
|
|
|
|
redirect_to announcement_index_path
|
|
|
|
else
|
|
|
|
render 'announcement/edit'
|
|
|
|
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] = "Announcement deleted successfully."
|
|
|
|
else
|
|
|
|
flash[:error] = "Failed to delete announcement."
|
|
|
|
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
|