相关疑难解决方法(0)

在ActiveModel中使用before_validation等的正确方法是什么

我应该延长或包括ActiveModel:Validations:Callbacks:ClassMethodsActiveModel:Validations:Callbacks

activemodel ruby-on-rails-3

12
推荐指数
1
解决办法
2097
查看次数

表单对象上的 Rails 验证数量失败

相关/固定: Ruby on Rails:表单对象验证不起作用

我有以下验证..

validates :age, numericality: { greater_than_or_equal_to: 0, 
                                only_integer: true, 
                                :allow_blank => true
                              }
Run Code Online (Sandbox Code Playgroud)

不是必须的,如果输入需要是数字。我注意到如果有人输入单词而不是数字,则提交并通过验证后字段值会更改为 0。我希望它是空白的或输入的值。

更新:

仍然没有解决方案,但这里有更多信息。

rspec测试
   it "returns error when age is not a number" do
      params[:age] = "string"
      profile = Registration::Profile.new(user, params)
      expect(profile.valid?).to eql false
      expect(profile.errors[:age]).to include("is not a number")
    end
Run Code Online (Sandbox Code Playgroud)

Rspec 测试失败:

 Registration::Profile Validations when not a number returns error when age is not a number

 Failure/Error: expect(profile.errors[:age]).to include("is not a number")
   expected [] to include "is not a number"
Run Code Online (Sandbox Code Playgroud)
2.6.5 :011 …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails ruby-on-rails-6

2
推荐指数
1
解决办法
421
查看次数