Tintin81
Tintin81

Reputation: 10207

How to upcase model names in localised Devise error messages?

I am using Devise in my Rails app and I wonder why it downcases all my model names in its error messages: Source code on Github

Is there any way to override this?

In German, for example, nouns start with capital letters, e.g. "Benutzer" is correct, but "benutzer" is wrong.

I have this in my devise.de.yml:

de:

  activerecord:
    models:
      user: "Benutzer"

However, Devise still renders Benutzer in small letters:

Konnte benutzer nicht speichern

This is wrong!

How can I fix this?

Thanks for any help...

Upvotes: 2

Views: 462

Answers (2)

Lazarus Lazaridis
Lazarus Lazaridis

Reputation: 6029

I think that you will have to overwrite the devise's method devise_error_messages! in a helper and put your logic there (remove the downcase or create cases per locales if you want downcase for other than German locale).

Upvotes: 0

Patrick Oscity
Patrick Oscity

Reputation: 54674

Quick fix

Add a file app/helpers/devise_helper.rb with the following contents:

module DeviseHelper
  def devise_error_messages!
    return "" if resource.errors.empty?

    messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
    sentence = I18n.t("errors.messages.not_saved",
                      :count => resource.errors.count,
                      :resource => resource.class.model_name.human)

    html = <<-HTML
    <div id="error_explanation">
      <h2>#{sentence}</h2>
      <ul>#{messages}</ul>
    </div>
    HTML

    html.html_safe
  end
end

:resource was forced to downcase before my edit:

resource.class.model_name.human.downcase

Source adapted from https://github.com/plataformatec/devise/blob/master/app/helpers/devise_helper.rb

Upvotes: 2

Related Questions