undefined局部变量或方法`root_path'

时间:2021-07-08 23:54:43

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

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

require 'spec_helper'

describe "home" do

 subject { page }

 before(:each) { visit root_path }

 describe "page" do
 it { should have_title('My Page')}
 end

end

When I run it I get

当我跑它时,我得到了

undefined local variable or method `root_path'

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

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

EDIT:

编辑:

My routes includes

我的路线包括

root "static#home"

EDIT 2:

编辑2:

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

重新打开此主题。将我的根声明移到顶部并没有解决它。

EDIT 3:

编辑3:

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

什么工作包括我的rspec配置中的url_helpers。我以前从来没有这样做过。任何人都可以回答为什么这有效

4 个解决方案

#1


32  

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

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

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

#2


10  

In your routes.rb, see if you have defined root. Something like this:

在routes.rb中,查看是否已定义root。像这样的东西:

root :to => 'home#index'

#3


3  

You figured out your issue, though I want to let others know that another reason they may get the error message...

你弄清楚了你的问题,但我想让别人知道他们可能收到错误信息的另一个原因......

NameError: undefined local variable or method `root_path'

...is because they renamed the "root" route:

...是因为他们重命名了“根”路线:

For example:

例如:

root to: 'pages#landing', as: :home

This will create home_path and home_url as named helpers in your application, whereas root_path and root_url will be undefined.

这将在您的应用程序中创建home_path和home_url作为命名助手,而root_path和root_url将是未定义的。

#4


1  

As mentioned by @emaillenin, you'll need to include root to: "controller#action" in your routes.rb file

正如@emaillenin所提到的,你需要在你的routes.rb文件中包含root:“controller#action”

However, you need to ensure this is declared correctly - I've had it before that root_path is not found, only because it's at the end of the routes.rb file or something

但是,你需要确保正确声明它 - 我在找不到root_path之前已经有了它,只是因为它位于routes.rb文件的末尾或者其他什么

It would be beneficial to show your entire routes.rb file

显示整个routes.rb文件会很有用

#1


32  

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

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

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

#2


10  

In your routes.rb, see if you have defined root. Something like this:

在routes.rb中,查看是否已定义root。像这样的东西:

root :to => 'home#index'

#3


3  

You figured out your issue, though I want to let others know that another reason they may get the error message...

你弄清楚了你的问题,但我想让别人知道他们可能收到错误信息的另一个原因......

NameError: undefined local variable or method `root_path'

...is because they renamed the "root" route:

...是因为他们重命名了“根”路线:

For example:

例如:

root to: 'pages#landing', as: :home

This will create home_path and home_url as named helpers in your application, whereas root_path and root_url will be undefined.

这将在您的应用程序中创建home_path和home_url作为命名助手,而root_path和root_url将是未定义的。

#4


1  

As mentioned by @emaillenin, you'll need to include root to: "controller#action" in your routes.rb file

正如@emaillenin所提到的,你需要在你的routes.rb文件中包含root:“controller#action”

However, you need to ensure this is declared correctly - I've had it before that root_path is not found, only because it's at the end of the routes.rb file or something

但是,你需要确保正确声明它 - 我在找不到root_path之前已经有了它,只是因为它位于routes.rb文件的末尾或者其他什么

It would be beneficial to show your entire routes.rb file

显示整个routes.rb文件会很有用