2010-09-06 48 views
1
validates_presence_of :match, 
    :message => "for your name and password could not be found. Did you #{link_to "forget your password", help_person_path}?" 

这就是我想要做的。我可以插入模型中设置的变量,但使用简单的Rails代码(如“link_to”)似乎不起作用。这是一个无赖。如何在验证消息中包含rails代码?

回答

2

您不能也不应该从模型中调用link_to方法。但是,您可以访问这样的命名路由:

class PostHack 
    include Rails.application.routes.url_helpers 
end 

class Post < ActiveRecord::Base 
    validates_presence_of :match, 
          :message => "<a href='#{PostHack.new.send :admin_posts_path}'>My link</a>" 
end 

非常黑客。我希望你不会使用它。

+0

我不会。我发现在登录页面上可能只有一个错误,所以我只是不显示模型错误并在我的视图中生成错误消息。 – chadoh 2010-09-08 11:08:01