Validation error messages without attribute

I am trying to show custom error messages with no attribute name in front. I used to do this with a custom_error_message gem, however it does not work with Rails 3.1

What am I doing now in create.js.erb:

alert("<%= @post.errors[:title] %>") 

What returns

 [&quot;Here goes my custom message?&quot;] 

My question is: how can I remove the brackets and & quot so that only the message remains. I embed it on a page using jquery.

+6
source share
1 answer

If you need errors on the base object, use not any specific attribute:

 errors.add(:base, "Here goes my custom message") 

As for the brackets and quotes, I think it depends on how you set your errors. When you just say @post.errors[:base] , it will return an array. This warning just literally spits out the array. You will probably want to sort through the errors or just grab .first if there is only one.

In addition, calling .html_safe will take care of the &quot; .

+18
source

Source: https://habr.com/ru/post/893801/


All Articles