Reputation:
I have this code in my update action of Registration controller. And I get undefined method 'errors'
. I can not use flash message here for some reason.
if subjects_selected.blank?
@registration = Registration.where(:student_id => params[:registration][:student_id], :semester_id => params[:registration][:semester_id] )
redirect_to editreg_registrations_path(@registration.first.id, params[:registration][:student_id], params[:registration][:semester_id]), @registration.errors.add(:You_have_to_register_for_at_least_one_subject) and return
end
How can I access error method here?
Upvotes: 1
Views: 79
Reputation: 165
you can take the method in Registration model for error displaying the errors like
error_array = Registration.validate_subjects(params[:registration][:student_id],params[:registration][:semester_id])
then in Registration model
def validate_subjects(student_id, semester_id)
is_registration = self.where(:student_id=>student_id,:semester_id =>semester_id)
error_array=[]
if !is_registration
//RIGHT THE CODE
error_array << "You_have_to_register_for_at_least_one_subjec"
end
error_array
end
Upvotes: 1
Reputation: 2786
if subjects_selected.blank?
@registration = Registration.where(:student_id => params[:registration][:student_id], :semester_id => params[:registration][:semester_id] )
if [email protected]
redirect_to editreg_registrations_path(@registration.first.id, params[:registration][:student_id], params[:registration][:semester_id])
else
@registration.errors.add(:You_have_to_register_for_at_least_one_subject)
end
end
Upvotes: 0