ruby-on-rails - Rails 验证不是文本字段的数字错误
问题描述
我创建了一个具有以下迁移的 AcademicInfo 模型。它有一个名为备注的列,该字段是文本字段。基本上我这样做是为了存储某种长文本。但是当我尝试用文本创建一个对象时,它给了我一种奇怪的“不是数字错误”
class CreateAcademicInfos < ActiveRecord::Migration[5.2]
def change
create_table :academic_infos do |t|
t.string :institution_name, null: false, default: ''
t.string :degree, null: false, default: ''
t.string :authority_body, null: false, default: ''
t.text :description_of_course, null: false, default: ''
t.date :start_date
t.date :end_date
t.text :remarks, null: false, default: ''
t.references :user, foreign_key: true
t.timestamps
end
end
end
我对控制器的请求消息是
{"institution_name": "Tribhuvan Uni", "authority_body": "Nepal Gov", "description_of_course": "Lamda Lamda description", "remarks": "THis is remarks", "start_date": "2018-12-12", "end_date": "2019-12-12", "degree": "Computer Sci", "user_id": 1}
但我得到了错误
{
"remarks": [
"is not a number"
]
}
在我的模型中,我有以下验证
class AcademicInfo < ApplicationRecord
validates :institution_name, :presence => true, :length => {:minimum => 3, :maximum => 100}
validates :degree, :presence => true, :length => {:minimum => 3, :maximum => 100}
validates :authority_body, :presence => true, :length => {:minimum => 3, :maximum => 100}
validates :description_of_course, :presence => true, :length => {:minimum => 3, :maximum => 100}
validates :remarks, :numericality => true, :presence => true, :length => {:minimum => 3, :maximum => 100}
validates :remarks, :length => {:minimum => 5}, :allow_blank => true
validates :start_date, :presence => true
validates :end_date, :presence => true
belongs_to :user
end
无法弄清楚为什么会这样。
解决方案
抱歉,输入数字是错误的:那里是真的
推荐阅读
- gis - Google Earth Engine 图层下载问题
- go - 如何打包 golang 测试助手代码?
- android - Firebase 未生成 FCM 令牌
- algorithm - 是否可以将所有 AVL 树转换为红黑树?
- react-native - 如何在 react-native 中创建图像对象?
- javascript - 如何对齐打印页面的内容?
- apache-spark - spark上的hive如何确定reducer数量?
- ios - 在应用程序处于后台状态时进行服务调用
- angular - 如何修复 Schema 验证失败并出现以下错误:在 ng serve 之后
- python - 将图像文本写入新的文本文件?