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:
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 "
issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With