使用自定义authlogic错误消息

时间:2009-12-16 23:18:41

标签: ruby-on-rails authentication authlogic

我正在使用authlogic gem在我的某个网站上进行用户验证。一切顺利,但我想知道是否可以更改用户输入无效电子邮件地址时返回的错误消息。

谢谢!

3 个答案:

答案 0 :(得分:16)

authlogic有一个特殊的设置用于此目的:

class UserSession < Authlogic::Session::Base  
  generalize_credentials_error_messages true
end

错误消息将是相同的:“电子邮件/密码组合无效”,密码或电子邮件是否错误。您可以更改指定字符串而不是true的消息文本:

generalize_credentials_error_messages "Try again"

答案 1 :(得分:14)

您可以使用validates_format_of_email_field_options覆盖电子邮件验证的设置。但是,如果您只想更改消息,则可以将选项与merge_validates_format_of_email_field_options合并,以便仅覆盖您指定的选项。您可以在用户控制器中指定设置,如下所示:

class User < ActiveRecord::Base
    acts_as_authentic do |c|
        c.merge_validates_format_of_email_field_options :message => 'My message'
    end
end

您还可以更改长度和唯一性验证的设置。还有更多其他设置,请查看documentation,在每个模块的:: Config部分中,您可以找到设置及其默认值以及如何覆盖它们。

或者,您可以使用本地化并设置error_messages.email_invalid(这是插件在将其设置为默认英语句子之前查找的内容,如果您正在构建国际应用程序,这也很有用。)

答案 2 :(得分:4)

通过更改en.yml文件来覆盖Authlogic错误消息 它对我有用。

en:
  authlogic:
      error_messages:
         login_blank: "Please enter the email address."
         login_not_found: "This email address is already in the system. Please choose a different email address."
         login_invalid: "Please enter a valid email address."