开发者

How can I include rails code inside a validation message?

validates_presence_of :match,
  :message => "for your name and password could not be found. Did you #{link_to "forget your password", help_person_path}?"

That's what I want to do. 开发者_运维问答I can interpolate variables that are set in the model, but using simple Rails code like "link_to" doesn't seem to work. Which is a bummer.


You can't and shouldn't call the link_to method from your models. However, you can get access to named routes like this:

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

Very hackish. I hope you will not use it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜