【问题标题】:Michael Hartl Rails Tutorial Chapter 3 Routing Errors on Local ServerMichael Hartl Rails 教程第 3 章本地服务器上的路由错误
【发布时间】:2014-07-29 22:53:11
【问题描述】:

这是第二次发生这种情况 - 一次是在使用 Ruby Mine 时,一次是在使用 Sublime Text 2 进行纯文本路由时。

起初一切都很好 - 本地主机加载正常,三个创建的静态页面加载正常 - 然后,繁荣,它只是坏了。我几乎 100% 肯定这会在我将主分支与静态页面分支合并后立即发生。静态页面不会加载(在任一分支上 - 我尝试来回切换以查看事情是否仍在一个分支上运行,但没有)。

相反,我收到以下错误:

没有路线匹配 [GET] "/home"

(同样的错误信息适用于其他两个页面)

这是主页的完整跟踪:

Rails.root: C:/Sites/sample

actionpack (4.0.8) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (4.0.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.8) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.8) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.0.8) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.0.8) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.0.8) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.0.8) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.0.8) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.8) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.8) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.0.8) lib/rails/engine.rb:511:in `call'
railties (4.0.8) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
I:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
I:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
I:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

Routes

Routes match in priority from top to bottom
Helper              HTTP Verb      Path                         Controller#Action
Path / Url          
static_pages_home_url   GET     /static_pages/home(.:format)    static_pages#home
static_pages_help_url   GET     /static_pages/help(.:format)    static_pages#help
static_pages_about_url  GET     /static_pages/about(.:format)   static_pages#about 

routes.rb 文件如下(与教程相符):

Sample::Application.routes.draw do
  get "static_pages/home"
  get "static_pages/help"
  get "static_pages/about"

end

static_pages_controller.rb 文件如下(再次与教程相符):

class StaticPagesController < ApplicationController

  def home
  end

  def help
  end

  def about
  end
end

我对此完全不知所措。

我发现了这个:Routing Error during "Ruby on Rails-Tutorial" 我仔细检查了,是的,当我启动服务器时,我在 C:\Sites\sample 中,所以我认为这不是问题,除非我合并后发生了什么这会改变我需要在哪里启动服务器?

我发现了这个:Rails Server Routing Error,但路由在我的 routes.rb 文件中,使用与教程中相同的语法。

如果需要,这里是 RSpec 用于测试的 static_pages_spec.rb 文件:

require 'spec_helper'

describe "StaticPages" do

  describe "Home page" do
    it "should have the content 'Sample App'" do
      visit '/static_pages/home'
      expect(page).to have_content('Sample App')
    end

    it "should have the title 'Home'" do
        visit '/static_pages/home'
        expect(page).to have_title("Ruby on Rails Tutorial Sample App | Home")
    end
  end

  describe "Help page" do
    it "should have the content 'Help'" do
        visit '/static_pages/help'
        expect(page).to have_content('Help')
    end

        it "should have the title 'Help'" do
        visit '/static_pages/help'
        expect(page).to have_title("Ruby on Rails Tutorial Sample App | Help")
    end
  end

  describe "About page" do
    it "should have the content 'About Us'" do
        visit '/static_pages/about'
        expect(page).to have_content('About Us')
    end

        it "should have the title 'About Us'" do
        visit '/static_pages/about'
        expect(page).to have_title("Ruby on Rails Tutorial Sample App | About Us")
    end
  end
end

【问题讨论】:

  • 看起来你有路由 GET staticpages/home,但没有路由 GET /home。是这样吗?
  • 是的 - 我认为我的大脑只是过度疲劳了。我完全相信我只是输入“/home”而不是“/static-pages/home”

标签: ruby-on-rails railstutorial.org


【解决方案1】:

你没有到/home 的路由,因此为什么你得到No route matches [GET] "/home" 你有一个到/static_pages/home 的路由(如果你在你的url 浏览器中输入它,你会看到你的主页)(即@987654325 @)

您可以将 /home 映射到 static_pages 主页,方法是将其添加到您的 routes.rb

get '/home', to: 'static_pages#home'

【讨论】:

  • 也许我的大脑刚刚在一天中的大部分时间里因为编码而爆炸。我可以发誓我只是在输入localhost:3000/home! (谈论让自己在全世界面前看起来像个白痴-嗯)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多