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
["Here goes my custom message?"]
My question is — how can I remove the brackets and the " so only the message is left. I’ll insert it into the page using jquery.
If you want errors on a base object, not a particular attribute use:
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.firstif there’s only one.Also, calling
.html_safewill take care of the"issue.