简体   繁体   中英

Can't Customize ActiveRecord Error Message Header

For some reason, I can successfully customize the specific error messages in ActiveRecord, but I can't seem to change the error message header.

en:
   activerecord:
    errors:
      template:
        header: 
          one: "Custom message goes here. 1 error prohibited this %{model} from being saved"
          other: "Customer message goes here. %{count} errors prohibited this %{model} from being saved"
      messages:
        blank: "custom :blank message goes here"
    models:
      complaint: "Complaint"
    attributes:
      complaint: 
        city: "Custom city name"
        neighborhood: "Custom neighborhood name"

If I leave city and neighborhood blank, I get the following error message:

2 errors prohibited this complaint from being saved:

  • Custom city name custom :blank message goes here

  • Custom neighborhood name custom :blank message goes here

For some reason the actual error messages are changed, but the error header is not. In debugging, I changed the YML file to the following, which did not change the default message:

en:
    activerecord:
      errors:
        template:
          header:
            one: "blah"
            other: "blah blah"
          body: "blah blah blah"

Does anyone have any idea why this simple change is not working? The only thing I can think of is that the "header" needs to be contextual to the model. Not sure.

References that I used:

[ http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models][1] [ http://guides.rubyonrails.org/v2.3.8/i18n.html#translations-for-active-record-models][1]

There seems to be a wrong spacing in your example. Please, check that there's always two spaces as indentation.

If that doesn't fix the problem, check the version of Rails that you're using:

Rails 2.3 example

pt:
  activerecord:
    errors:
      template:
        header:
          one: "não foi possível guardar este %{model} porque encontramos um erro"
          other: "não foi possível guardar este %{model} porque encontramos%{count} erros"
        # The variable :count is also available
        body: "Encontramos problemas nos seguintes campos:"

      # The values :model: "attribute and :value are always available for interpolation
      # The value :count is available when applicable. Can be used for pluralization.
      messages:
        inclusion: "não esta incluído na lista"
        exclusion: "está reservado"
        invalid: "não é válido"

Rails 3 example

es:
  errors: &errors
    format: ! '%{attribute} %{message}'
    messages:
      ...
      taken: ya está en uso
      too_long: es demasiado largo (%{count} caracteres máximo)
      too_short: es demasiado corto (%{count} caracteres mínimo)
      wrong_length: no tiene la longitud correcta (%{count} caracteres exactos)
    template:
      body: ! 'Se encontraron problemas con los siguientes campos:'
      header:
        one: No se pudo guardar este/a %{model} porque se encontró 1 error
        other: No se pudo guardar este/a %{model} porque se encontraron %{count} errores
  activemodel:
    errors:
      <<: *errors
  activerecord:
    errors:
      <<: *errors

The problem was that the error message header is actually hard-coded starting in Rails 3.0. From a resource I found:

Look at the app/views/users/_form.html.erb generated by scaffold. The error message is hard coded. So, changing the locale file has no effect.

Actually, the Rails 3.0 does not utilize the activerecord.errors.template.header translation. In the Rails 2.x, this translation is used in the error_messages method of the form builder, but this method is deprecated and extracted as a plugin.

If you are curious, do the following:

-rails plugin install git://github.com/joelmoss/dynamic_form.git

-Edit app/views/_form.html.erb as follows:

<%= form_for(@user) do |f| %>
  <%= f.error_messages %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Source https://github.com/svenfuchs/rails-i18n/issues/118

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM