Ruby-on-rails 在 Model 类中查看 helper link_to

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4713571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:07:54  来源:igfitidea点击:

View helper link_to in Model class

ruby-on-railsruby-on-rails-3

提问by Krule

Using Rails 3, is there a way to use link_to helper, or any helper for that matter, inside model?

使用 Rails 3,有没有办法在模型内部使用 link_to 帮助程序或任何与此相关的帮助程序?

回答by Chuck Vose

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.

出于某些原因,您可能需要在模型中使用 link_to。是的,@andy,这违反了 MVC,但这并不意味着您应该因不回答问题而获得积分。

@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.

@schwabsauce,比这更容易。如果您在初始化程序或其他东西中执行,则第一行甚至不是绝对必要的。同样的事情适用于 .sanitize 和 .raw 以及一大堆其他很棒的功能。

ActionView::Base.send(:include, Rails.application.routes.url_helpers)
ActionController::Base.helpers.link_to(whatever)

If you want to use autopaths you may have to do this inside your link_to:

如果您想使用自动路径,您可能必须在 link_to 中执行此操作:

Rails.application.routes.url_helpers.page_path(@page)

回答by rantler

Be very careful following the advice outlined in Chuck's post if you're doing this in Rails 3.2.1 . It would seem as though that approach is not a safe way to go about including the link_to helper in non-view classes in Rails 3.2.1. There is a safer way (that works for us in any case) outlined below.

如果您在 Rails 3.2.1 中执行此操作,请非常小心地遵循 Chuck 的帖子中概述的建议。在 Rails 3.2.1 的非视图类中包含 link_to 助手似乎不是一种安全的方法。下面概述了一种更安全的方法(在任何情况下都对我们有用)。

When we used the approach in Chuck's post in one of our classes, it ended up having very troubling and difficult to debug consequences. It ended up causing side effects / bugs that only turned up in very specific (and rare) circumstances.

当我们在我们的一个课程中使用 Chuck 的帖子中的方法时,它最终产生了非常麻烦且难以调试的后果。它最终导致了仅在非常特定(且罕见)的情况下才会出现的副作用/错误。

The problem, as far as we can tell, is that this line:

据我们所知,问题在于这一行:

ActionView::Base.send(:include, Rails.application.routes.url_helpers)

Is telling ActionView::Baseto include the Rails.application.routes.url_helpers, which ActionView::Baseapparently already does on its own. Having it include the url_helpersa second time, seems to cause re-initialization of the routes state (@_routes in classes that have included the ActionDispatch::Routing::UrlFor module).

正在告诉ActionView::Base包括Rails.application.routes.url_helpers,这ActionView::Base显然已经独立完成了。让它url_helpers第二次包含,似乎会导致路由状态的重新初始化(@_routes 在包含 ActionDispatch::Routing::UrlFor 模块的类中)。

This leads to seemingly random and unexplained "undefined method 'url_for' for nil:NilClass" exceptions in views that attempt to call, directly or indirectly, the url_for method afterActionView::Basehas included the url_helpersthe second time.

这导致看似随机且无法解释的“undefined method 'url_for' for nil:NilClass”异常在试图直接或间接调用 url_for 方法第二次ActionView::Base包含的视图中url_helpers

The solution that worked for us was instead of telling ActionView::Baseto include the url_helpersagain, just include the UrlHelpermodule yourself wherever you might need it.

这对我们工作的解决方案是,而不是告诉ActionView::Base给包括url_helpers再次,仅仅只包含UrlHelper自己的模块,无论你可能需要它。

Then when you need to use link_to and have access to the path you can simply do this (assuming login_path is valid for your app):

然后,当您需要使用 link_to 并访问路径时,您可以简单地执行此操作(假设 login_path 对您的应用程序有效):

include ActionView::Helpers::UrlHelper
...
link = link_to('here', Rails.application.routes.url_helpers.login_path)

It took us a very long time and quite a lot of head scratching to track down the bugs caused by the double-include and I just wanted to warn others to be careful when tweaking the behavior of the Rails base classes.

我们花了很长时间和相当多的头脑来追踪由双重包含引起的错误,我只是想警告其他人在调整 Rails 基类的行为时要小心。

回答by schwabsauce

I got this to work with the following inclusions:

我让它与以下包含物一起工作:

include ActionView::Helpers::UrlHelper
include ActionController::UrlFor
include Rails.application.routes.url_helpers

cattr_accessor :controller
def controller; self.class.controller; end
def request; controller.request; end

Then in my controller I populated the attribute (creating a controller from scratch requires a significant amount of data in the arguments hash).

然后在我的控制器中填充属性(从头开始创建控制器需要参数哈希中的大量数据)。

Lead.controller = self

回答by bbozo

link_to helper, MVC violation

link_to helper,MVC 违规

What Andy said,

安迪说的,

if you're generating HTML in the model you probably need to take a big long look at what you're doing and why.

如果您在模型中生成 HTML,您可能需要仔细查看您在做什么以及为什么。

URL helpers

网址助手

URLs on the other hand often come in handy outside of view-controller code, in all kinds of service/form/api/... classes for example, even in models if you have to.

另一方面,URL 通常在视图控制器代码之外派上用场,例如在各种服务/表单/api/... 类中,甚至在模型中(如果必须的话)。

Yes, Rails.application.routes.url_helpers is a module, but that doesn't mean you should just include it wherever or funny stuff will start happening as Gary put it:

是的,Rails.application.routes.url_helpers 是一个模块,但这并不意味着你应该将它包含在任何地方或者有趣的事情将开始发生,正如 Gary 所说:

https://www.destroyallsoftware.com/blog/2011/one-base-class-to-rule-them-all

https://www.destroyallsoftware.com/blog/2011/one-base-class-to-rule-them-all

What you can do is:

你可以做的是:

    delegate :url_helpers, :to => 'Rails.application.routes'

and then use, for example

然后使用,例如

    url_helpers.home_url

回答by Andy Lindeman

Not without hackery.

并非没有黑客。

If you think you need link_toin a model, you're likely violating some principle of the Model-View-Controller architecture.

如果您认为需要link_to在模型中使用,那么您可能违反了模型-视图-控制器架构的某些原则。

A model should be a place for data and business logic, but generating links is almost certainly a job for the controller or view (or, Rails specifically, in a helper class).

模型应该是存放数据和业务逻辑的地方,但生成链接几乎肯定是控制器或视图(或者,Rails,特别是在辅助类中)的工作。