我正在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>
答案 0 :(得分:1)
您可以对重复的错误消息使用常量,它将起作用。
class User < ActiveRecord::Base
has_secure_password validations: false
has_many :posts
USERNAME_ERROR_MESSAGE = "Usernames can only contain letters, digits, dashes and underscores, and should be between 2 and 15 characters long. Please choose another."
PASSWORD_ERROR_MESSAGE = "Passwords should be a least 4 characters long. Please choose another."
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 }
end
此外,还有一个问题:如果每个字段的多个验证条件失败,则会重复出现错误消息。一种解决方案是编写自定义验证方法,如下所示:http://guides.rubyonrails.org/active_record_validations.html#custom-methods
答案 1 :(得分:0)
尝试使用类方法:
validates :name,
presence: { message: Proc.new { username_error_message } },
uniqueness: { case_sensitive: false, message: Proc.new { username_error_message } },
length: { minimum: 2, maximum: 15, message: Proc.new { username_error_message } }
validates :password,
presence: { message: Proc.new { password_error_message } },
length: { minimum: 4, message: Proc.new { password_error_message } }
private
def self.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 self.password_error_message
"Passwords should be a least 4 characters long. Please choose another."
end
答案 2 :(得分:0)
根本问题在于您在定义之前访问方法。这个问题本身没有任何关于它们是私有,常量,实例方法还是类方法的事情。
正在定义您的User
类时引用这些方法。引用存在于散列构造函数{}
中,作为参数传递给方法调用validates
,这意味着在validates
被调用时正在评估 。这不像是你将一个块传递给validates
以后再进行评估。
如果您在之前将定义移动到,那么它将引用它们,它将解决这个基本问题,但您也可以将它们定义为实例方法并将它们作为类方法引用。您需要使您的定义模式符合您的访问模式(例如,将方法定义为类方法)。
(注意:除了其他答案中提到的字符串之外,还有许多其他选项可用于定义和引用这些字符串,包括类变量,类局部变量等。它们主要在于它们在何处以及如何你甚至可以使用实例变量和方法,虽然在这种情况下这很奇怪,需要你实例化一个对象才能引用它们。重点是Ruby有很多可能性,值得研究/理解它们。当你有时间的时候。)
答案 3 :(得分:-1)
试试这个
class User < ActiveRecord::Base
has_secure_password validations: false
has_many :posts
validates :name,
presence: { message: self.username_error_message },
uniqueness: { case_sensitive: false, message: self.username_error_message },
length: { minimum: 2, maximum: 15, message: self.username_error_message }
validates :password,
presence: { message: self.password_error_message },
length: { minimum: 4, message: self.password_error_message }
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
private :username_error_message, :password_error_message
我刚加了一些“自我”。在方法调用之前给它一个这个实例的上下文。 我还改变了私有声明方法