Tracing down routing errors in Rails
I am going through my first RoR tutorial and came up on an action controller routing error. I have checked my code at least 8 times, but can't figure out the issue. I'm sure this will happen again in the future. My question is, in general, how should I go about solving these errors? If it's relevant I am using RVM, Rails 3.0.1 and Ruby 1.9.2.
For reference to what I am currently dealing with, here are some of the files:
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
Terminal Output
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)
Also ran rake routes
and got
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"}
You don't have a route for /pages/home
defined anywhere. You only have matched root /
to PagesController
and its home
method. So requesting /
will work, but /pages/home
not.
You either need to define:
match "/pages/home" => "pages#home"
or add a resource Pages with additional method home
:
resources :pages do
get "home", :on => :collection
end
Here're some useful routing resources:
- http://guides.rubyonrails.org/routing.html
- http://railscasts.com/episodes/203-routing-in-rails-3
As @Matt said, you haven't defined a /pages/home
route, it's only matched to /
.
The best piece of advise I can give you when dealing with routing problems is to run rake routes
in the terminal (where you would run rails server
etc.), which outputs a list of all recognized routes for your app.
if you're following the Ruby on Rails tutorial, it says to delete index.html
http://ruby.railstutorial.org/chapters/filling-in-the-layout#top
精彩评论