views:

25

answers:

2

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"}
+1  A: 

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:

Matt
I made this change in the layout_links_spec.rb file:require 'spec_helper'describe "LayoutLinks" do it "should have a Home page at '/home'" do get '/home' response.should have_selector('title', :content => "Home") endAnd my pages_controller.rb has:class PagesController < ApplicationController def home @title = "Home" end
zkidd
Your test in the comment now matches your `rake routes` output. But both don't match the `routes.rb` **in the question**. Where's your matching for `/home`?
Matt
+1  A: 

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.

Jeriko
@jeriko, I thought I did route the home page with "root :to => 'pages#home'"
zkidd
You routed `/` (root) to the `home` action of `PagesController`. There is no mention of `/pages/home`, so rails doesn't know how to deal with that URL. Bear in mind that multiple URLs can be routed to the same controller/action pair.
Jeriko