我在 Rails 中编写了一个黑客新闻克隆来学习框架,并遇到了在模型中调用辅助方法的问题:
class User < ActiveRecord::Base
has_secure_password validations: false
has_many :posts
validates :name,
presence: { message: username_error_message },
uniqueness: { case_sensitive: false, message: username_error_message },
length: { minimum: 2, maximum: 15, message: username_error_message }
validates :password,
presence: { message: password_error_message },
length: { minimum: 4, message: password_error_message }
private
def username_error_message
"Usernames can only contain letters, digits, dashes and underscores, and should be between 2 and 15 characters long. Please choose another."
end
def password_error_message
"Passwords should be a least 4 characters long. Please choose another."
end
end
我收到以下错误(Rails 4):
undefined local variable or method `username_error_message' for #<Class:XXX>