Ruby on Rails的:如何让显示的来自子资源的错误信息? [英] Ruby on Rails: how to get error messages from a child resource displayed?

查看:105
本文介绍了Ruby on Rails的:如何让显示的来自子资源的错误信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有困难的时候了解如何让Rails的,以示对被验证失败时,我渲染XML模板子资源精确的错误信息。可以想像,我有以下类:

I'm having a difficult time understanding how to get Rails to show an explicit error message for a child resource that is failing validation when I render an XML template. Hypothetically, I have the following classes:

class School < ActiveRecord::Base
    has_many :students
    validates_associated :students

    def self.add_student(bad_email)
      s = Student.new(bad_email)
      students << s
    end
end

class Student < ActiveRecord::Base
    belongs_to :school
    validates_format_of :email,
                  :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i,
                  :message => "You must supply a valid email"
end

现在,在控制器,让我们说我们要建立一个简单的API来允许我们添加一个新的学校与学生在这(再次,我说,这是一个可怕的例子,但效力于的目的及其作用这个问题)

Now, in the controller, let's say we want to build a trivial API to allow us to add a new School with a student in it (again, I said, it's a terrible example, but plays its role for the purpose of the question)

class SchoolsController < ApplicationController
    def create
      @school = School.new
      @school.add_student(params[:bad_email])
      respond_to do |format|
          if @school.save
          # some code
          else
            format.xml  { render :xml => @school.errors, :status => :unprocessable_entity }
          end
      end
    end
end

现在的验证工作得很好,东西死,因为电子邮件这是否在在学生类的validates_format_of方法设置正则表达式不匹配。不过,我得到的输出如下:

Now the validation is working just fine, things die because the email doesn't match the regex that's set in the validates_format_of method in the Student class. However the output I get is the following:

<?xml version="1.0" encoding="UTF-8"?>
<errors>
  <error>Students is invalid</error>
</errors>

我想,我上面设置有validates_format_of更有意义的错误消息出现。意思是,我希望它说:

I want the more meaningful error message that I set above with validates_format_of to show up. Meaning, I want it to say:

 <error>You must supply a valid email</error>

我在做什么错了不显示?

What am I doing wrong for that not to show up?

推荐答案

添加一个验证模块的学校模型合并错误:

Add a validation block in the School model to merge the errors:

class School < ActiveRecord::Base
  has_many :students

  validate do |school|
    school.students.each do |student|
      next if student.valid?
      student.errors.full_messages.each do |msg|
        # you can customize the error message here:
        errors.add_to_base("Student Error: #{msg}")
      end
    end
  end

end

现在 @ school.errors 将包含正确的错误:

Now @school.errors will contain the correct errors:

format.xml  { render :xml => @school.errors, :status => :unprocessable_entity }

注意:

您没有添加新的学生上学需要一个单独的方法,请使用以下语法:

You don't need a separate method for adding a new student to school, use the following syntax:

school.students.build(:email => email)

更新的Rails 3.0 +

errors.add_to_base 已退的Rails 3.0及以上版本,并应改为:

Update for Rails 3.0+

errors.add_to_base has been dropped from Rails 3.0 and above and should be replaced with:

errors[:base] << "Student Error: #{msg}"

这篇关于Ruby on Rails的:如何让显示的来自子资源的错误信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆