Retrospring/app/controllers/announcement_controller.rb

49 lines
1.1 KiB
Ruby
Raw Normal View History

2020-04-19 12:12:22 -07:00
class AnnouncementController < ApplicationController
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 12:58:57 -07:00
if Announcement.update(params[:id], announcement_params)
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