Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何更改rails上的错误消息_Ruby On Rails - Fatal编程技术网

Ruby on rails 如何更改rails上的错误消息

Ruby on rails 如何更改rails上的错误消息,ruby-on-rails,Ruby On Rails,我的看法是: <div class="form-group"> <% if @product.errors.details[:amount].any? %> <div class="has-error"> <%= f.label "#{t('product.shineer_irsen')}", class: 'control-label' %> &l

我的看法是:

<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>

我想您想要翻译的值必须大于或等于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')}
您可以在此处找到更多信息:


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

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

这样,您就不需要更改视图。

您应该在模型中进行验证。在模型中:验证:数量,数值性:{大于或等于:0}。但一旦我犯了错误,就会变成。我的控制器中没有显示方法。我删除了,因为我不需要它,所以,有一些问题:将你的模型添加到帖子中,我会帮助你。我看不到你的模型的验证,它在哪里?哦,糟糕,我从备份中得到了。我试过了。但是没有起作用。仍然收到相同的错误消息。验证完全正确。但是把消息放在语言环境文件而不是模型上是一种更好的做法。我必须从输入中获得大于或等于0的值,并且必须在模型中进行验证。我必须同时做这两件事。@CarlosCastillo我看着你们回答,学习AR的翻译肯定很好。在模型中,这个def amount=new_值。。。结束方法,我将输入值添加到金额中。如果我不在视图中验证,他们可以输入一些负值。如果该值小于或等于旧金额值。我没有收到错误。很难阅读注释中的代码,请将问题中的所有代码发布到@tuvsheezorigo
#{t('product.shineer_irsen')}
validates :age, numericality: {greater_than_or_equal_to: 0, message: 'Este campo tiene que ser positivo' }