secret
Created

students controller

  • Download Gist
students_controller.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
class StudentsController < ApplicationController
 
before_filter :signed_in_coach
before_filter :admin_coach, only: :destroy
 
def index
@students = Student.paginate(page: params[:page],
per_page: 25).order('created_at DESC')
@active_nav = @page_header = "Registry"
end
 
def new
@student = Student.new
@coaches = Coach.all
@active_nav = @page_header = "New Student"
end
 
def show
@student = Student.find(params[:id])
@page_header = @student.student_name
@current_lesson = @student.lessons.find_by_number(@student.current_lesson)
@current_reminder = @student.reminders.last
@commentable = @student
@comments = @commentable.comments
@comment = Comment.new
end
 
def create
if multi_coach_env?
@coach = Coach.find_by_id(params[:coach][:id])
@student = @coach.students.build(params[:student])
else
@student = current_coach.students.build(params[:student])
end
@student.current_lesson = 1
@student.last_contact_at = Time.now
if @student.save
flash[:success] = %Q[<i class="fa fa-check fa-fw"></i> Student created and assigned to #{@student.coach.first_name}!].html_safe
redirect_to @student
else
@active_nav = @page_header = "New Student"
flash.now[:error] = %Q[<i class="fa fa-times fa-fw"></i> Oops, we've had a problem. Please correct the highlighted fields below.].html_safe
render 'new'
end
end
 
def edit
@student = Student.find(params[:id])
@coaches = Coach.all
@page_header = @student.student_name
end
 
def update
if multi_coach_env?
# Handle update for multi-coach env
@student = Student.find(params[:id])
@coach = Coach.find_by_id(params[:coach][:id])
# raise
if @student.update_attributes(params[:student])
@student.coach = @coach
@student.save!
flash[:success] = %Q[<i class="fa fa-check fa-fw"></i> Student record saved!].html_safe
redirect_to @student
else
@coaches = Coach.all
@page_header = @student.student_name
flash.now[:error] = %Q[<i class="fa fa-times fa-fw"></i> Oops, we've had a problem. Please correct the highlighted fields below.].html_safe
render 'edit'
end
else
# Handle save for single-coach env
@student = Student.find(params[:id])
# raise
if @student.update_attributes(params[:student])
flash[:success] = %Q[<i class="fa fa-check fa-fw"></i> Student record saved!].html_safe
redirect_to @student
else
@page_header = @student.student_name
flash.now[:error] = %Q[<i class="fa fa-times fa-fw"></i> Oops, we've had a problem. Please correct the highlighted fields below.].html_safe
render 'edit'
end
end
end
 
def destroy
student = Student.find(params[:id])
student.destroy
flash[:success] = %Q[<i class="fa fa-check fa-fw"></i> Student #{student.student_name} (ID: #{student.id}) has been deleted.].html_safe
redirect_to students_url
end
 
def search
session[:return_to] ||= request.referer
if params[:search].to_i.zero?
flash[:error] = %Q[<i class="fa fa-times fa-fw"></i> Please enter a numeric student ID.].html_safe
redirect_to session.delete(:return_to)
else
id = params[:search].to_i.abs
@student = Student.search(id).first
if @student
redirect_to @student
else
flash[:caution] = %Q[<i class="fa fa-warning fa-fw"></i> Sorry, we couldn't find a student with ID #{id}.].html_safe
redirect_to session.delete(:return_to)
end
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.