user464180
user464180

Reputation: 1359

Rails: Change displayed error column names

I've tried following the posts found here: Rails 3 Change Error Message and here: ActiveRecord validates... custom field name but neither has worked for me and my database field names still are displayed.

For example I get: Name is too short (minimum is 1 characters)

Any thoughts/Is there a preferred way to troubleshoot this? Thanks.

Here is what I have using the first linked article in my en locale:

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

Here is a bit of my account model:

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

attr_accessible :name,  :admin_attributes, :image

After a failed save attempt on the account, here is the code to display errors from my view:

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

Upvotes: 1

Views: 2571

Answers (1)

d3vkit
d3vkit

Reputation: 1982

I know this is quite old, but I found this question before finally just turning to the rails guides, which got my code working.

It looks like you want to change the attribute name for Account, but are using Order:

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

I don't believe you need the models: account: "Account" either. This is to tell rails if you want to call the model something different, but you don't, so you can remove it. Attributes then takes the model for what you want to change, and then the attribute.

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

I think what may have thrown you off is that it almost looks like attributes belongs to models, but it's actually on a different line. Hope this helps, as late as it is!

Upvotes: 3

Related Questions