简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM