Rails的验证虚拟属性 [英] Rails validating virtual attributes

查看:118
本文介绍了Rails的验证虚拟属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这个模型:

class Bunny < ActiveRecord::Base
    attr_accessor :number
    validates_presence_of :number
    validates_numericality_of :number
end

每当我提交表单创建这个模型我得到以下错误:

Whenever I submit a form to create this model I get the following error:

未定义的方法`number_before_type_cast'的#&LT;兔子:0x103624338&GT;

undefined method `number_before_type_cast' for #<Bunny:0x103624338>

推荐答案

我加入这个方法我的兔子模型解决了这一问题:

I fixed the problem by adding this method to my Bunny model:

def number_before_type_cast
    number
end

我不喜欢它,但我想它会工作,直到有人张贴一个更好的解决方案。

I don't like it, but I suppose it will work until someone posts a better solution.

这篇关于Rails的验证虚拟属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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