Jadam
Jadam

Reputation: 1775

root route not working

In routes.rb file: root "pages#index"

pages_controller.rb file

  class PagesController < ApplicationController
    def index
      render 'home'
    end

    def about
    end
  end

view named home.html.haml

rake routes returns

Prefix Verb URI Pattern        Controller#Action
   home GET  /home(.:format)    pages#home
  about GET  /about(.:format)   pages#about

localhost just says "We're sorry, but something went wrong.", the console says ActionController::RoutingError (No route matches [GET] "/home"):

On Rails 4.2.0.beta2

*Edit for Rails version and console error

Upvotes: 0

Views: 1964

Answers (2)

Benjamin J. B.
Benjamin J. B.

Reputation: 1181

Indeed, rake routes says that a GET /home will match pages#home, the home action in pages controller.

Can you show us more of your routes.rb file ?

Moreover, why don't you rename your home.html.haml file to index.html.haml file, thus your index action can become

def index
end

If you don't specify which view to render, Rails will try to find a view with the name of your action.

Upvotes: 1

Diego Couto
Diego Couto

Reputation: 585

I would update your routes.rb file to:

root 'pages#index'

get '/home',  to: 'pages#index'
get '/about', to: 'pages#about'

As suggested by Benjamin, I do think that it would be a better design to rename your home.html.erb to index.html.erb. This way you can remove the render 'home' from your index method.

What is odd is that your rake routes output says that /home will be redirected to pages#home, which is a controller method that you don't have.

Upvotes: 0

Related Questions