【问题标题】:How to change change error messages on rails如何更改rails上的更改错误消息
【发布时间】: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


【解决方案1】:

我想你想翻译“值必须大于或等于 0”,如果是这种情况,你需要做的是在语言环境文件上为它创建一个翻译。在西班牙语中会是这样的:

# config/locales/es.yml
es:
  activerecord:
    errors:
      models:
        product:
          attributes:
            amount:
              greater_than_or_equal_to: 'What ever you want to say'

根据您的母语,您必须创建文件并定义消息,我认为您已经这样做了,因为您正在使用翻译:

#{t('product.shineer_irsen')}

您可以在此处找到更多信息:

http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models

【讨论】:

  • 我试过了。但没有奏效。仍然收到相同的错误消息。
【解决方案2】:

您可以自定义从模型验证中获得的错误消息,在这种情况下您需要添加到您的模型中:

validates :age, numericality: {greater_than_or_equal_to: 0, message: 'Este campo tiene que ser positivo' }

有了这个,你不需要改变视图。

【讨论】:

  • 验证完全正确。但最好将消息放在语言环境文件而不是模型上。
  • 我想我的问题很糟糕。我必须从输入中获得大于或等于 0 的值,并且我必须在模型中进行验证。我必须两者都做。
  • @CarlosCastillo 我在看你回答,真的很好(肯定会学习 AR 翻译)。
  • 在模型中,使用这个 def amount=(new_value) ... end 方法,我将输入值添加到数量中。如果我没有在视图中验证,他们可以输入一些负值。如果该值小于或等于旧金额值。我没有收到错误。
  • 评论里的代码看不懂,把问题里的所有代码贴出来@TuvsheeZorigoo
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-10
  • 2021-07-25
  • 2021-09-15
  • 1970-01-01
相关资源
最近更新 更多