What is the Rails3 version of errors.add_to_base?

前端 未结 4 1281
情话喂你
情话喂你 2021-02-03 19:46

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         


        
相关标签:
4条回答
  • 2021-02-03 19:55

    Try to use in below format,

    errors[:base] << "Your Message"

    It wont insert anything before your validation messages.

    0 讨论(0)
  • 2021-02-03 20:10

    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.

    0 讨论(0)
  • 2021-02-03 20:13

    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.

    0 讨论(0)
  • 2021-02-03 20:15

    This should work in rails 3.1.3:

    errors.add :base, "message"
    
    0 讨论(0)
提交回复
热议问题