class User < ActiveRecord::Base
before_save { self.email = email.downcase }
(validates :name, presence: true, length: { maximum: 50 })
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 };
format: { with: VALID_EMAIL_REGEX };
uniqueness: { case_sensitive: false },
has_secure_password:
(validates :password, presence: true, length:{ minimum: 6 } )
end
有人帮我弄清楚这种语法出错的地方
答案 0 :(得分:1)
在User
模型中试试这个:
class User < ActiveRecord::Base
before_validation { self.email = email.downcase! }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
end
答案 1 :(得分:0)
您的模型应如下所示。
class User < ActiveRecord::Base
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length:{ minimum: 6 }
has_secure_password
end
答案 2 :(得分:0)
您可能还想更改此行:
before_save { self.email = email.downcase }
进入这个:
before_validation { self.email = email.downcase }
这样做的方式是:
因此,用户将在其电子邮件中使用大写字母,然后保存,并且由于使用大写字母,验证将失败。
因此,您希望在验证之前将其电子邮件设置为小写字母,而不是在以下情况之后: - )