开发者

What is the Rails3 version of errors.add_to_base?

I want to write a custom validation in Rails 3, but following this example I get:

'ActiveModel::DeprecatedErrorMethods.add_to_base' call is deprecated in Rails 3.0

What is the rails3 version of:

def validate
   开发者_StackOverflow中文版errors.add_to_base "If you are attaching a file you must enter a label for it" if !attachment.blank? and attachment_label.blank?
end


This should work in rails 3.1.3:

errors.add :base, "message"


From http://apidock.com/rails/ActiveRecord/Errors/add_to_base:

Use model_instance.errors[:base] << "Msg" instead of deprecated model_instance.errors.add_to_base("Msg") for Rails 3.


For me this "hack" worked best:

instance.errors.add("", "Msg")

When I tried to specify "base" as the first argument, the word base kept getting inserted into my validation messages.


Try to use in below format,

errors[:base] << "Your Message"

It wont insert anything before your validation messages.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜