Hartl的教程第5.3.2节:Rails路由

时间:2012-10-05 18:51:38

标签: ruby-on-rails routes match railstutorial.org static-pages

我一直在教程中完全按照它编写的方式进行操作。到目前为止,一切都顺利完成,直到本节。

我应该将config / routes.rb文件中的“GET”语句更改为以下内容:

SampleApp::Application.routes.draw do
  root to: 'static_pages#home'

  match '/help',    to: 'static_pages#help'
  match '/about',   to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact'

这应该让测试通过。他们没有通过。它们继续失败,并出现以下错误,作为9个类似错误之一:

Failure/Error: visit about_path
NameError:
   undefined local variable or method 'about_path' ....

我不知道如何让它通过,以便我可以继续前进。我错过了什么?哈特尔错过了什么?其他提出这个问题的人从来没有得到任何有意义的答案,甚至在尝试时都有效。

在有人要求之前:

所有版本的Rails,Ruby和其他已安装的组件都与本教程中使用的版本完全相同,因为它是在2012-10-05编写的。一切都与教程完美匹配。

更新:这是当前的static_pages_spec.rb文件

require 'spec_helper'

describe "Static pages" do

  describe "Home page" do

    it "should have the h1 'Sample App'" do
      visit root_path
      page.should have_selector('h1', text: 'Sample App')
    end

    it "should have the base title" do
      visit root_path
      page.should have_selector('title',
                        text: "Ruby on Rails Tutorial Sample App")
    end

    it "should not have a custom page title" do
      visit root_path
      page.should_not have_selector('title', text: '| Home')
    end
  end

  describe "Help page" do

    it "should have the h1 'Help'" do
      visit help_path
      page.should have_selector('h1', text: 'Help')
    end

    it "should have the title 'Help'" do
      visit help_path
      page.should have_selector('title',
                        text: "Ruby on Rails Tutorial Sample App | Help")
    end
  end

  describe "About page" do

    it "should have the h1 'About'" do
      visit about_path
      page.should have_selector('h1', text: 'About Us')
    end

    it "should have the title 'About Us'" do
      visit about_path
      page.should have_selector('title',
                    text: "Ruby on Rails Tutorial Sample App | About Us")
    end
  end

  describe "Contact page" do

    it "should have the h1 'Contact'" do
      visit contact_path
      page.should have_selector('h1', text: 'Contact')
    end

    it "should have the title 'Contact'" do
      visit contact_path
      page.should have_selector('title',
                    text: "Ruby on Rails Tutorial Sample App | Contact")
    end
  end
end

Rake Routes结果:

   root  /                  static_pages#home
   help  /help(.:format)    static_pages#help
   about  /about(.:format)   static_pages#about
   contact  /contact(.:format) static_pages#contact

6 个答案:

答案 0 :(得分:3)

尝试更改您的路线。请将您的路线改为:

match '/about',   to: 'static_pages#about', as: 'about'

并且您还应该重新加载Spork以便更改申请。 您还可以添加:

load "#{Rails.root}/config/routes.rb"

进入Spork.each_run块,以便每次运行Spork时重新加载路由。

答案 1 :(得分:1)

重装spork为我工作。

答案 2 :(得分:0)

您可以尝试在规范中添加此内容。

describe "Static pages" do
include Rails.application.routes.url_helpers
.......

答案 3 :(得分:0)

我认为你应该输入浏览器

http://localhost:3000/
例如,

而不是http://localhost:3000/static_pages/home来获取主页。 我认为问题不是代码,而是如何访问页面。在本教程5.3.2之前的部分中,您需要访问localhost:3000/static_pages/home才能访问主页。按照5.3.2中的说明操作后,只需键入http://localhost:3000/

答案 4 :(得分:0)

我刚刚遇到这个错误,并且有一些帮助解决这个问题。我错误地在layouts / _header.html.erb和static_pages / home.html.erb上添加了signin_path和signup_path。这让每个页面都对signin_path是什么感到困惑。

我犯错的另一个问题是根路径。如果从示例应用程序目录运行'rm public / index.html',routes.rb中的'root'static_pages #home'应该可以正常工作。问题是公共目录中的内容会在您自定义时覆盖应用程序的任何其他部分。希望这有助于你帮助我:)

答案 5 :(得分:0)

默认情况下,指定的路线在规格中不可用。将以下代码添加到spec_helper.rb文件中:

RSpec.configure do |config|
  config.include Rails.application.routes.url_helpers
end
相关问题