在 Model 类中查看 helper link_to [英] View helper link_to in Model class
问题描述
使用 Rails 3,有没有办法在模型内部使用 link_to 帮助程序或任何与此相关的帮助程序?
Using Rails 3, is there a way to use link_to helper, or any helper for that matter, inside model?
推荐答案
您可能出于某些原因需要在模型中使用 link_to.是的,@andy,这违反了 MVC,但这并不意味着您应该因不回答问题而获得积分.
There are some reasons that you may need link_to in a model. Yes, @andy, it's a violation of MVC, but that doesn't mean you should get points for not answering the question.
@schwabsauce,比这更容易.如果您在初始化程序或其他东西中执行,则第一行甚至不是绝对必要的.同样的事情适用于 .sanitize 和 .raw 以及一大堆其他很棒的功能.
@schwabsauce, it's easier than that. The first line isn't even strictly necessary if you do it in an initializer or something. Same thing works for .sanitize and .raw and a whole load of other awesome functions.
ActionView::Base.send(:include, Rails.application.routes.url_helpers)
ActionController::Base.helpers.link_to(whatever)
如果您想使用自动路径,您可能必须在 link_to 中执行此操作:
If you want to use autopaths you may have to do this inside your link_to:
Rails.application.routes.url_helpers.page_path(@page)
这篇关于在 Model 类中查看 helper link_to的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!