开发者

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 go开发者_JAVA技巧es 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜