Create a gist now

Instantly share code, notes, and snippets.

students controller
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment