Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation error messages without the attribute

I'm trying to show custom error messages without the attribute name in front. I used to do this with the custom_error_message gem, however it doesn't work with Rails 3.1

What I'm trying now in create.js.erb:

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

Which returns

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

My question is -- how can I remove the brackets and the &quot so only the message is left. I'll insert it into the page using jquery.

like image 826
Martin Petrov Avatar asked Jul 28 '11 17:07

Martin Petrov


1 Answers

If you want errors on a base object, not a particular attribute use:

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

As for the brackets and quotes, I guess it depends how you're setting your errors. When you just say @post.errors[:base] it will return an array. This alert is just literally spitting out the array. You'd probably want to iterate over the errors or just grab .first if there's only one.

Also, calling .html_safe will take care of the &quot; issue.

like image 185
brad Avatar answered Sep 23 '22 19:09

brad