我正在浏览我的第一个RoR tutorial并提出了一个动作控制器路由错误。我已经检查了我的代码至少8次,但无法弄清楚问题。我相信这将在未来再次发生。我的问题是,一般来说,我应该如何解决这些错误?如果它是相关的我使用RVM,Rails 3.0.1和Ruby 1.9.2。
有关我目前正在处理的内容的参考,以下是一些文件:
pages_controller.rb
class PagesController < ApplicationController
def home
@title = "Home"
end
def contact
@title = "Contact"
end
def about
@title = "About"
end
def help
@title = "Help"
end
end
layout_links_spec.rb
require 'spec_helper'
describe "LayoutLinks" do
it "should have a Home page at '/'" do
get '/'
response.should have_selector('title', :content => "Home")
end
it "should have a Contact page at '/contact'" do
get '/contact'
response.should have_selector('title', :content => "Contact")
end
it "should have an About page at '/about'" do
get '/about'
response.should have_selector('title', :content => "About")
end
it "should have a Help page at '/help'" do
get '/help'
response.should have_selector('title', :content => "Help")
end
end
的routes.rb
SampleApp::Application.routes.draw do
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
root :to => 'pages#home'
end
终端输出
Started GET "/pages/home" for 127.0.0.1 at 2010-10-21 06:51:01 -0400
ActionController::RoutingError (No route matches "/pages/home"):
Rendered /Users/zak/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.1/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.8ms)
同时运行rake routes
并获得
ZKidds-MacBook-Pro:sample_app zak$ rake routes
(in /Users/zak/rails_projects/sample_app)
contact /contact(.:format) {:controller=>"pages", :action=>"contact"}
about /about(.:format) {:controller=>"pages", :action=>"about"}
help /help(.:format) {:controller=>"pages", :action=>"help"}
home /home(.:format) {:controller=>"pages", :action=>"home"}
答案 0 :(得分:1)
您没有在任何地方定义/pages/home
的路线。您只有根/
与PagesController
及其home
方法匹配。因此请求/
会有效,但/pages/home
不会。
您需要定义:
match "/pages/home" => "pages#home"
或使用其他方法home
添加资源页面:
resources :pages do
get "home", :on => :collection
end
以下是一些有用的路由资源:
答案 1 :(得分:1)
正如@Matt所述,您尚未定义/pages/home
路线,它只与/
匹配。
我在处理路由问题时可以给你的最好的建议是在终端中运行rake routes
(你可以运行rails server
等),这会输出所有已识别路由的列表为您的应用程序。
答案 2 :(得分:1)
如果您正在关注Ruby on Rails教程,它会删除index.html
http://ruby.railstutorial.org/chapters/filling-in-the-layout#top