引擎规范中使用正确的

引擎规范中使用正确的

本文介绍了在 Rails 引擎规范中使用正确的 url_for 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:31