首页 > 解决方案 > 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

无法弄清楚为什么会这样。

标签: ruby-on-railsactiverecordruby-on-rails-5rails-migrations

解决方案


抱歉,输入数字是错误的:那里是真的


推荐阅读