问题描述
我在Rails引擎中有一个请求规范。呈现的视图调用路线并传递哈希,即 projects_path(:scope => user)
。这样的路由最终将调用 url_for
,但是在许多地方都定义了 url_for
。当运行应用程序或在主应用程序(根级别)中运行请求规范时,调用链最终在 ActionView :: RoutingUrlFor#url_for
处结束;但是,当在引擎中运行规范时,调用链最终位于 ActionView :: Helpers :: UrlHelper#url_for
。
I have a request spec in a Rails engine. The view that is rendered calls a route and passes in a hash, i.e. projects_path(:scope => "user")
. A route like this will eventually call url_for
, but url_for
is defined in many places. When running the application or running a request spec in the main app (root level), the call chain ends up at ActionView::RoutingUrlFor#url_for
; when running a spec in the engine, however, the call chain ends up at ActionView::Helpers::UrlHelper#url_for
.
在Rails 4中,在 UrlHelper
中定义的 url_for
方法不再接受哈希参数,所以我留下此错误
In Rails 4, the url_for
method defined in UrlHelper
no longer accepts hash arguments, so I'm left with this error
Failure/Error: visit projects_opportunity_intakes_path
ActionView::Template::Error:
arguments passed to url_for can't be handled. Please require routes or provide your own implementation
我要弄清楚的是为什么会这样当我在引擎中运行某个规范时,便会使用 ActionView :: Helpers :: UrlHelper
,但在其他情况下则不会。这很令人困惑。对我来说很突出的一件事:当我在视图中放置一个断点并评估 self.class
时,我通常会得到从 ActionView :: Base
,但是当我从引擎运行规范时,该类不会继承任何东西。我不知道这是否相关,但是似乎很可疑。
What I'm trying to figure out is why the heck it's falling back on ActionView::Helpers::UrlHelper
when I run a spec in my engine, but not in any other case. This is quite perplexing. One thing that stands out to me: when I put a breakpoint in my view and evaluate self.class
, I normally get something that inherits from ActionView::Base
, but when I run a spec from my engine, the class doesn't inherit from anything. I don't know if that's related, but it seems fishy.
有人经历过吗?知道解决方法吗?我的模块会以错误的顺序包含在内吗?在 spec_helper
或虚拟应用程序初始化中是否可以执行某些操作,以确保使用了正确的模块?
Anyone experience this? Know a way around it? Could my modules be getting included in the wrong order? Is there something in spec_helper
or in the dummy app initialization that I can do to make sure that the right modules are being used?
推荐答案
在更新到Rails 4时,我遇到了同样的问题,我发现问题是一个包含以下行的模型:
I had the same issue when updating to Rails 4, and I discovered that the problem was a model containing the following line:
include Rails.application.routes.url_helpers
尝试在您的应用程序中查找包含 url_helpers
的代码。删除该行应有助于解决问题。
Try finding code in your application that includes url_helpers
somewhere. Removing that line should help solve the problem.
这篇关于在Rails引擎规范中使用正确的url_for方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!