app/controllers/announcements_controller.rb
# == Schema Information
#
# Table name: announcements
#
# id :integer not null, primary key
# title :string
# content :text
# attachments_count :integer
# limit_to_users :text
# start_delivering_at :datetime
# stop_delivering_at :datetime
# created_at :datetime not null
# updated_at :datetime not null
#
class AnnouncementsController < ApplicationController
before_action :authenticate_user!
before_action :set_announcement, only: [:show, :edit, :update, :destroy]
# GET /announcements
# GET /announcements.json
def index
@announcements = Announcement.all
end
# GET /announcements/1
# GET /announcements/1.json
def show
@announcement_comments = @announcement.comments.includes(:user)
# @announcement_comments=[]
# @announcement_comment=@announcement.comments.build
end
# GET /announcements/new
def new
@announcement = Announcement.new
3.times do
@announcement.announcement_attachments.build
end
end
# GET /announcements/1/edit
def edit
end
# POST /announcements
# POST /announcements.json
def create
@announcement = Announcement.new(announcement_params)
respond_to do |format|
if @announcement.save
format.html { redirect_to @announcement, notice: 'Announcement was successfully created.' }
format.json { render :show, status: :created, location: @announcement }
else
format.html { render :new }
format.json { render json: @announcement.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /announcements/1
# PATCH/PUT /announcements/1.json
def update
respond_to do |format|
if @announcement.update(announcement_params)
format.html { redirect_to @announcement, notice: 'Announcement was successfully updated.' }
format.json { render :show, status: :ok, location: @announcement }
else
format.html { render :edit }
format.json { render json: @announcement.errors, status: :unprocessable_entity }
end
end
end
# DELETE /announcements/1
# DELETE /announcements/1.json
def destroy
@announcement.destroy
respond_to do |format|
format.html { redirect_to announcements_url, notice: 'Announcement was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_announcement
@announcement = Announcement.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def announcement_params
params.require(:announcement).permit(:title, :content, :limit_to_users, :start_delivering_at, :stop_delivering_at, announcement_attachments_attributes: [:_destroy, :id, :image])
end
end