【问题标题】:Rails validation not a number error for text fieldRails 验证不是文本字段的数字错误
【发布时间】:2019-01-13 14:27:09
【问题描述】:

我创建了一个具有以下迁移的 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-rails activerecord ruby-on-rails-5 rails-migrations


    【解决方案1】:

    对不起,把数字写错了:那里是真的

    【讨论】:

      猜你喜欢
      • 2012-12-19
      • 2016-11-24
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多