launchscout / nku

NKU Class Spring
5 stars 14 forks source link

Stuck between a rock and a hard place #66

Closed beisert1 closed 10 years ago

beisert1 commented 10 years ago

Not sure whats going on with my code. I have everything set up correctly code wise, however, student is able to log attendance more than once a day, and all of the students show up as absent and not in class even after logging attendance.

My attendances controller:

class AttendancesController < ApplicationController
  def new
    @attendance = Attendance.new
  end

  def create
    @current = current_student

    @attendance = Attendance.new
    @attendance.seat = params[:attendance][:seat]
    @attendance.attended_on = Date.today
    @attendance.student_id = @current.id

    @existingAttendance = Attendance.where(:attended_on => Date.today,
      :student_id => @current.id)

    if(@existingAttendance.first == nil)
      @attendance.save
      redirect_to attendances_path, :notice => "You have successfully logged your attendance."
    else
      flash[:error] = "You have already created an attendance for today."
      render "new"
    end

  end

  def show
    @attendance = Attendance.find(params[:id])
  end

  def index
    @attendances = Attendance.all
  end
end
staffordw1 commented 10 years ago

Try moving:

    @attendance = Attendance.new
    @attendance.seat = params[:attendance][:seat]
    @attendance.attended_on = Date.today
    @attendance.student_id = @current.id

underneath your conditional

if(@existingAttendance.first == nil)

See if that works.

beisert1 commented 10 years ago

When I put it underneath it gave me an error:

image

When I put it inside the conditional it doesn't change anything

beisert1 commented 10 years ago

seems like none of my validation logic is working, I can even put a 6 as my seat number or even a non numerical number with no error.

class Attendance < ActiveRecord::Base
    belongs_to :student

  validates :seat, :inclusion => 1..4
  validates :seat, :numericality => {:only_integer => true}
  validates :seat, presence: true
end
staffordw1 commented 10 years ago

I meant between @attendance.save and the condition, like this:

if(@existingAttendance.first == nil)
    @attendance = Attendance.new
    @attendance.seat = params[:attendance][:seat]
    @attendance.attended_on = Date.today
    @attendance.student_id = @current.id

    @attendance.save
    redirect_to attendances_path, :notice => "You have successfully logged your attendance."
else
...

Sorry, I guess I wasn't really specific.

staffordw1 commented 10 years ago

As for the validations, I did all that in the controller. I'm not sure that's the right way to do it but it's the only way I was able to get the correct flash notices to appear...

beisert1 commented 10 years ago

Putting it under the conditional like that didn't change anything unfortunately. I will try putting the validations in the controller thanks for the help @staffordw1

beisert1 commented 10 years ago

I figured out that even though I can enter a wrong number or non numerical number as my seat number it wont put it on the attendances unless its a valid seat number but I get no flash error message like what I put in my code to do.

beisert1 commented 10 years ago

I am guessing something is going wrong with this portion because its not going to the else statement.

  if(@existingAttendance.first == nil)
      @attendance.save
      redirect_to attendances_path, :notice => "You have successfully logged your attendance."
    else
      flash[:error] = "You have already created an attendance for today."
      render "new"
    end
staffordw1 commented 10 years ago

Yeah, I tried something like:

validates :seat, presence: true, :notice => "You entered nothing."

And I was getting errors and I didn't know how to fix them. If you just use notices in the controller, as long as you have

<% flash.each do |name, msg| -%>
      <%= content_tag :div, msg, class: name %>
<% end -%>

in your new.html.erb in your attendance folder, they should show up.

You should be able to get away with just @existingAttendance == nil instead of @existingAttendance.first. It's possible that @existingAttendance.first is returning nil where @existingAttendance wouldn't... but I'm just making guesses really, I don't know for sure

beisert1 commented 10 years ago

Changing it to @existingAttendance == nil worked!! Thank you @staffordw1