开发者

How can I include rails code inside a validation message?

开发者 https://www.devze.com 2023-01-15 14:54 出处:网络
validates_presence_of :match, :message => \"for your name and password could not be found. Did you #{link_to \"forget your password\", help_person_path}?\"
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

精彩评论

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

关注公众号