rails 路由语法错误

rails routing syntax errors

我正在 railstutorial.org 编写 Michael Hartl 的教程。我在第 5 章中遇到了使路由工作的困难。 如果我从路由文件开始

routes.rb

Rails.application.routes.draw do
    root 'static_pages#home'
    get 'static_pages/help'  
    get 'static_pages/about'
    get 'static_pages/contact'

对于其中的每一个都有一个测试,例如

static_pages_controller_test.rb

test "should get home" do
    get :home
    assert_response :success
    assert_select "title", "Ruby on Rails Tutorial Sample App"
end

此语法有效且所有测试均通过,但后来他想使用 *_path 约定更改语法。

所以现在测试看起来像

class StaticPagesControllerTest < ActionController::TestCase
  test "should get home" do
  get root_path
  .
  .
  end

test "should get help" do
  get help_path
  .
  .
  end

并且我将路线更新为

root 'static_pages#home'
get  '/help',    to: 'static_pages#help'
get  '/about',   to: 'static_pages#about'
get  '/contact', to: 'static_pages#contact'

但现在所有测试都失败并显示消息

ERROR["test_should_get_home", StaticPagesControllerTest, 2016-06-30     05:02:41 -0700]
test_should_get_home#StaticPagesControllerTest (1467288161.43s)
ActionController::UrlGenerationError:           ActionController::UrlGenerationError: 
No route matches {:action=>"/", :controller=>"static_pages"}

ERROR["test_should_get_help", StaticPagesControllerTest, 2016-06-30 05:02:41 -0700]
test_should_get_help#StaticPagesControllerTest (1467288161.43s)
ActionController::UrlGenerationError:          ActionController::UrlGenerationError: 
No route matches {:action=>"/help", :controller=>"static_pages"}

我的控制器看起来像这样

class StaticPagesController < ApplicationController
  def home
  end

  def help
  end
  .
  .
end

如果我 运行 我得到

 Prefix Verb URI Pattern        Controller#Action
   root GET  /                  static_pages#home
   help GET  /help(.:format)    static_pages#help
  about GET  /about(.:format)   static_pages#about
contact GET  /contact(.:format) static_pages#contact

我做错了什么?

您需要重写这些路由,以便它可以根据您的测试为您创建动态路由助手。像这样写,

get 'static_pages/help'    , as: :help  
get 'static_pages/about'   , as: :about
get 'static_pages/contact' , as: :contact

阅读3.6 Naming Routes

根据您当前的路线,那些 *_path 将像 static_pages_aboutstatic_pages_help 等。我不确定您是如何获得像您这样的 rake routes 输出的已显示未使用 as 选项。

我不太确定,但是如果你这样做会发生什么:

root 'static_pages#home'
get  'help',    to: 'static_pages#help'
get  'about',   to: 'static_pages#about'
get  'contact', to: 'static_pages#contact'

是的作者上周用 5.0.0 更新了 rails 教程。建议你也更新一下,这样以后的旅程会更愉快,不会出错,另外,你会在5.0.0中得到更多新东西

更新您的 tests/controllers/static_pages_controller_test.rb

require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest

  test "should get home" do
    get root_path
    assert_response :success
    assert_select "title", "Ruby on Rails Tutorial Sample App"
  end

  test "should get help" do
    get help_path
    assert_response :success
    assert_select "title", "Help | Ruby on Rails Tutorial Sample App"
  end

  test "should get about" do
    get about_path
    assert_response :success
    assert_select "title", "About | Ruby on Rails Tutorial Sample App"
  end

  test "should get contact" do
    get contact_path
    assert_response :success
    assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
  end
end

我相信一旦你更新你的 tests/controllers/static_pages_controller_test.rb 你会看到绿色测试 $ rails test