未定义的局部变量或方法“root

编程入门 行业动态 更新时间:2024-10-11 23:24:10
本文介绍了未定义的局部变量或方法“root_path"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的 rspec 文件中有以下代码块位于/spec 文件夹的根目录.

I have the following block of code in my rspec file located in the root of the /spec folder.

require 'spec_helper' describe "home" do subject { page } before(:each) { visit root_path } describe "page" do it { should have_title('My Page')} end end

当我运行它时,我得到

undefined local variable or method `root_path'

这没有任何意义.当我遵循 rails 教程时,类似的设置工作得很好.有人可以帮忙吗?

Which doesn't make any sense. When I followed the rails tutorial a similar set up worked just fine. Can anyone help?

我的路线包括

root "static#home"

编辑 2:

重新打开这个话题.将我的根声明移到顶部并没有解决问题.

Reopening this topic. Moving my root declaration to the top did not fix it.

编辑 3:

在我的 rspec 配置中包含 url_helpers 是有效的.我以前从来没有这样做过.谁能回答为什么这有效?

What worked was including url_helpers in my rspec config. I've never had to do this before. can anyone answer why this worked?

推荐答案

默认情况下,规范中不提供命名路由.将以下代码添加到 spec_helper.rb:

Named routes are not available in specs by default. Add the following code to spec_helper.rb:

RSpec.configure do |config| config.include Rails.application.routes.url_helpers end

更多推荐

未定义的局部变量或方法“root

本文发布于:2023-10-27 23:40:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534870.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   局部   未定义   方法   root

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!