validates :presence vs validates_presence_of using rails 3

后端 未结 1 1483
时光取名叫无心
时光取名叫无心 2021-01-01 11:59

I have a couple of simple models that are associated like so:

MODELS

class Task < ActiveRecord::Base
  belongs_to :user
  validates :name, :presen         


        
相关标签:
1条回答
  • 2021-01-01 12:37

    When you use the newer validates :name format, you can put multiple validations in one line rather than having to have multiple lines for each type of validation. Because of this, when Rails hits your :message parameter, it thinks it's a validation method rather than a message associated with :presence. Try this instead:

    validates :name, :presence => {:message => 'Name cannot be blank, Task not saved'}
    

    Also, depending on how you display your errors, this error may actually show up as 'Name Name cannot be....'; if so, you'll want to set the message to just 'cannot be blank, Task not saved'.

    0 讨论(0)
提交回复
热议问题