Closed beisert1 closed 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.
When I put it underneath it gave me an error:
When I put it inside the conditional it doesn't change anything
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
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.
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...
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
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.
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
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
Changing it to @existingAttendance == nil worked!! Thank you @staffordw1
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: