我的觀點:如何改變在軌道上改變錯誤信息
<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
一些線的本地/ 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}。但是一旦我收到錯誤,http:// localhost:3000/products/2/edit會變爲http:// localhost:3000/products/2。我的控制器中沒有顯示方法。我刪除了,因爲我不需要它所以,有一些問題:) –
添加您的模型的帖子,我會幫你 –