【发布时间】:2021-10-30 23:41:54
【问题描述】:
想要实现
感谢收看。
我正在使用 Rails 进行开发。
我已经在模型上设置了验证,但是最大字符数的验证不能正常工作。
公司.rb
validates :price,
length: { maximum: 20 },
presence: true,
numericality: true
这样写,
然后使用表单输入 20 个字符(10000000000000000000)时将触发验证。
顺便说一句, 19 个字符(1000000000000000000)也使验证失败,并且 18 个字符(100000000000000000)通过了验证。
Form.vue
<template>
<main>
<form>
<fieldset>
<legend>price</legend>
<div class="form-row">
<b-form-input class="form-control" type="text"></b-form-input>
</div>
</fieldset>
</form>
</main>
</template>
schema.rb
create_table "companies", id: :bigint, unsigned: true, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
・
・
・
t.decimal "price", precision: 24, scale: 4
・
・
・
参数(当输入 20 个字符并捕获验证时)
Parameters: {"price"=>"10000000000000000000"}
看参数的时候是字符串,所以在控制器接收参数的地方改了,还是验证失败。
def create_company_params
params.require(:company).permit(
).tap do |v|
v[:price] = v[:price].to_i
end
end
我不明白为什么验证中的最大字符数与我在模型中设置的不同。
如果您有任何建议,请告诉我。
环境
红宝石 2.6 Ruby on Rails 6.0
【问题讨论】:
标签: ruby-on-rails