警告错误ActionController :: UrlGenerationError:

时间:2015-06-05 14:07:03

标签: ruby-on-rails unit-testing guard

我开始为我的rails应用程序编写测试,并计划在构建实际页面之前编写它们,但我遇到了麻烦。我搜索了堆栈溢出和谷歌,并尝试了几件事。该路由在我的routes.rb文件中指定,如下所示:

resources :welcome

我将以下内容写入我的测试代码中。

require 'rails_helper'
require 'spec_helper'

RSpec.describe "Welcomes", type: :request do
  it "checks the welcome page." do
    visit welcome_path
  end
end

我从后卫那里得到以下错误:

Failures:

  1) Welcomes checks the welcome page.
     Failure/Error: visit welcome_path
     ActionController::UrlGenerationError:
       No route matches {:action=>"show", :controller=>"welcome"} missing required keys: [:id]
     # ./spec/requests/welcomes_spec.rb:6:in `block (2 levels) in <top (required)>'

Finished in 0.00347 seconds (files took 1.61 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/requests/welcomes_spec.rb:5 # Welcomes checks the welcome page.

2 个答案:

答案 0 :(得分:1)

visit welcome_path更改为get '/welcome'

答案 1 :(得分:0)

Rails index路由集的resources位于welcomes_pathwelcome_path会引用特定的Welcome,并且需要:id参数。

如果欢迎页面只是一个目标网页,而不是实际上的资源,您可能只希望config/routes.rb包含:

get 'welcome', to: 'welcome#index`

第一个welcome是路径(host.com/welcome),to:参数是controller#action

Rails routing docs很棒。如果这没有意义,请给他们一个很好的阅读。