0

I am trying to tweak Devise helper in order to show the error message only, not the field name.

Here is what Devise code looks like:

messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join

My problem is that it shows both the field name and message like:

Phone number Phone number is missing

I want the message only

EDIT EDIT

My actual model phone number validation looks like this :

validates :phone_number, presence: {message: "Nous avons besoin de votre numéro de téléphone"}
validates :phone_number, format: { with: /\A(?:[+\d].*\d|\d)\z/, message: "le format de votre numéro de téléphone n'est pas reconnu" }

Basically the presence message is "We require your phone number" which makes something like :

phone number Nous avons besoin de votre numéro de téléphone

(I know I am not using locales but I will move everything in fr.yml once it is ok)

Maxence
  • 2,029
  • 4
  • 18
  • 37
  • Can you post your model code please so we can see the validations? `full_messages` by default should be `#{field_name} #{message}` so you shouldn't see the duplication of the field like that. Additionally, by default, a presence check validation (`validates :phone_number, presence: true`) should have an error message of `can't be blank` rather than `is missing`. – ejdraper Jan 08 '18 at 17:32
  • Hi @ejdraper I have edited my question. Is it not possible to get rid of the `#{field_name}` bit ? Or should I just add a locale for `phone_number` to get proper translation and keep it as a headline of my message – Maxence Jan 08 '18 at 17:39
  • Possible duplicate of [rails error message displays key and I only want value](https://stackoverflow.com/questions/22027442/rails-error-message-displays-key-and-i-only-want-value) – Anurag Aryan Jan 08 '18 at 20:26
  • Right, I see what you mean now. I think if you want to have a completely custom message, where the field name is in there but not necessarily prepended at the beginning, then you most likely have to use the custom message (with locale translation as needed like you say), and then use your solution below to display the message only - any other customizations generally assume you want the field name name at the beginning. – ejdraper Jan 08 '18 at 21:38

1 Answers1

0

Ok seems I have found an answer:

resource.errors.each do |attr,message|
   messages << "<li>"+message+"</li>"
end

It only add the error message without the model name to the messages constant. Though it is a bit ugly.

Maxence
  • 2,029
  • 4
  • 18
  • 37