class SchedulesController < ApplicationController def index @schedule = Schedule.order(:hour, :minute) @modbus_running = Modbus::PollingService.running? end def new @schedule = Schedule.new end def create @schedule = Schedule.new(schedule_params) if @schedule.save redirect_to schedule_edit_schedules_path, notice: "스케쥴이 추가 되었습니다." else error_messages = @schedule.errors.full_messages.join(", ") redirect_to schedule_edit_schedules_path, alert: "스케쥴 추가 실패: #{error_messages}" end end def destroy @schedule = Schedule.find_by(id: params[:id]) if @schedule.destroy redirect_to schedule_edit_schedules_path, notice: "스케줄이 삭제되었습니다." else error_messages = @schedule.errors.full_messages.join(", ") redirect_to schedule_edit_schedules_path, alert: "스케쥴 삭제 실패: #{error_messages}" end end def reset Schedule.delete_all ActiveRecord::Base.connection.execute("DELETE FROM sqlite_sequence WHERE name='schedules'") Rails.application.load_seed redirect_to schedules_path, notice: "스케줄이 초기화되었습니다." end def schedule_edit @schedule = Schedule.order(:hour, :minute) end def schedule_edit_update error_messages = [] params[:schedule].each do |id, attributes| schedule = Schedule.find_by(id: id) next unless schedule unless schedule.update( hour: attributes[:hour], minute: attributes[:minute], is_active: attributes[:is_active], temperature: attributes[:temperature] ) error_detail = schedule.errors.full_messages.join(", ") time_label = "#{attributes[:hour]}시 #{attributes[:minute]}분" error_messages << "#{time_label} - #{error_detail}" end end if error_messages.any? redirect_to schedules_path, alert: error_messages.join("
") else redirect_to schedules_path, notice: "스케줄이 성공적으로 업데이트되었습니다." end end private def schedule_params params.require(:schedule).permit(:hour, :minute, :is_active, :temperature) end end