加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何在Rails 3中的功能测试中使用polymorphic_

发布时间:2020-12-17 02:29:53 所属栏目:百科 来源:网络整理
导读:我正在尝试在Rails 3中的功能测试中使用polymorphic_path. 起初我会得到 NoMethodError: undefined method `polymorphic_path' for #ArticlesControllerTest:0x492f17c 然后我补充道 include Rails.application.routes.url_helpers 未定义的方法错误已停止,
我正在尝试在Rails 3中的功能测试中使用polymorphic_path.

起初我会得到

NoMethodError: undefined method `polymorphic_path' for #<ArticlesControllerTest:0x492f17c>

然后我补充道

include Rails.application.routes.url_helpers

未定义的方法错误已停止,但现在常规路径(例如article_path(article))已停止工作:

NameError: undefined local variable or method `default_url_options' for #<ArticlesControllerTest:0x33ccbe0>
.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/action_dispatch/testing/assertions/routing.rb:175:in `method_missing'
.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/action_dispatch/routing/url_for.rb:102:in `url_options'
.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/action_dispatch/routing/url_for.rb:131:in `url_for'
.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.9/lib/action_dispatch/routing/route_set.rb:195:in `article_path'

我曾经能够通过包含在Rails 2中正常使用polymorphic_path

include ActionController::UrlWriter

如何在Rails 3中使用它?

解决方法

我需要包括:

include ActionDispatch::Routing::UrlFor
include Rails.application.routes.url_helpers

并设置:

default_url_options[:host] = 'www.example.com'

我通过这篇文章发现了一个类似的问题
http://steve.dynedge.co.uk/2010/04/29/rails-3-rake-and-url_for/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读