1

我已尝试关注此处找到的帖子: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 %> 
4

1 回答 1

3

我知道这已经很老了,但我在最终转向 rails guides之前发现了这个问题,这让我的代码正常工作。

看起来您想更改 Account 的属性名称,但使用的是 Order:

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

我不相信你需要模型: account: "Account" 。这是为了告诉 Rails 如果你想给模型起不同的名字,但你不这样做,所以你可以删除它。然后,Attributes 将模型用于您要更改的内容,然后是属性。

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

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

于 2012-09-26T17:49:11.047 回答