【问题标题】:Rails: Change displayed error column namesRails:更改显示的错误列名称
【发布时间】:2012-06-20 16:40:52
【问题描述】:

我已尝试关注此处找到的帖子:Rails 3 Change Error Message 和此处:ActiveRecord validates... custom field name,但两者都没有为我工作,我的数据库字段名称仍然显示。

例如我得到:名称太短(最少为 1 个字符)

任何想法/是否有解决此问题的首选方法?谢谢。

这是我在本地环境中使用第一篇链接文章的内容:

en:
  activerecord:
    models:
      account:        "Account"
    attributes:
      order:
        name:         "Business Name"

这是我的帐户模型:

validates_presence_of :name
validates_length_of :name, :minimum => 1, :maximum => 100

attr_accessible :name,  :admin_attributes, :image

在帐户尝试保存失败后,以下是我认为显示错误的代码:

<% if @account.errors.any? %>
      <div class="errorExplanation">
        <h2>Errors encountered with your account information:</h2>
        <ul>
          <% @account.errors.full_messages.each do |m| %>
            <li><%= m %></li>
          <% end %>
        </ul>
      </div>
    <% end %> 

【问题讨论】:

    标签: ruby-on-rails-3 validation error-handling


    【解决方案1】:

    我知道这已经很老了,但我在最后才发现这个问题turning to the rails guides,这让我的代码正常工作。

    您似乎想更改 Account 的属性名称,但正在使用 Order:

    en:
      activerecord:
        models:
          account:        "Account"
        attributes:
          order: # Right here, this should be account
            name:         "Business Name"
    

    我认为您也不需要模型:帐户:“帐户”。这是为了告诉 rails 如果你想给模型起不同的名字,但你没有,所以你可以删除它。然后,Attributes 将模型用于您要更改的内容,然后是属性。

    en:
      activerecord:
        attributes:
          account:
            name: "Business name"
    

    我认为可能让您感到厌烦的是,它几乎看起来属性属于模型,但实际上它在另一条线上。希望这会有所帮助,因为它是迟到的!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-04
      • 1970-01-01
      • 2013-09-08
      • 2019-05-19
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多