未定义的方法`错误'

时间:2014-01-09 06:03:37

标签: ruby-on-rails

我在注册控制器的更新操作中有此代码。我得到了未定义的方法'errors'。出于某种原因,我不能在这里使用flash消息。

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

如何在此处访问错误方法?

2 个答案:

答案 0 :(得分:1)

您可以使用注册模型中的方法来显示错误的错误 喜欢

error_array = Registration.validate_subjects(params[:registration][:student_id],params[:registration][:semester_id])

然后在注册模型

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

答案 1 :(得分:0)

if subjects_selected.blank?
    @registration = Registration.where(:student_id => params[:registration][:student_id], :semester_id => params[:registration][:semester_id] )

   if !@registration.errors     
   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