【发布时间】:2017-04-04 17:57:41
【问题描述】:
我的看法:
<div class="form-group">
<% if @product.errors.details[:amount].any? %>
<div class="has-error">
<%= f.label "#{t('product.shineer_irsen')}", class: 'control-label' %>
<%= f.number_field :amount, value: 0, min: 0, class: "form-control" %>
</div>
<% else %>
<%= f.label "#{t('product.shineer_irsen')}", class: 'control-label' %>
<%= f.number_field :amount, value: 0, min: 0, class: "form-control" %>
<% end %>
</div>
我想验证输入字段中的金额,并且我想将其错误消息更改为我的母语。
现在,错误消息是 error message
如何改变它?请帮帮我。
产品型号:
class Product < ApplicationRecord
belongs_to :item
belongs_to :user
belongs_to :branch
validates :amount, numericality: {greater_than_or_equal_to: 0}
def item_name
item.try(:name)
end
def item_name=(query)
self.item = Item.find_by_name(query) if query.present?
end
def amount=(new_value)
if read_attribute(:amount)
@old_amount = read_attribute(:amount)
write_attribute(:amount, new_value.to_i + @old_amount)
else
write_attribute(:amount, new_value.to_i)
end
end
end
local/mn.yml 的某行
activerecord:
attributes:
...
errors:
models:
subcategory:
attributes:
category_id:
invalid: "ahaha"
blank: "хоосон байж болохгүй"
category:
blank: "сонгоогүй байна."
product:
attributes:
amount:
greater_than_or_equal_to: 'Оруулах утга 0-ээс их байх ёстой.'
【问题讨论】:
-
你应该在模型中验证
-
我做到了。在模型中:验证:数量,数量:{greater_than_or_equal_to: 0}。但是一旦我出错,localhost:3000/products/2/edit 就会变成localhost:3000/products/2。我的控制器中没有显示方法。我删除了,因为我不需要它所以,有一些问题:)
-
将您的模特添加到帖子中,我会帮助您
-
我在你的模型上看不到验证,它在哪里?
-
糟糕,我是从备份中得到的。
标签: ruby-on-rails