Raj
Raj

Reputation: 396

Routing error in Ruby on Rails 3

I am new to Ruby on Rails I am getting this error

uninitialized constant WelcomeController

after creating the sample project. I enabled

root :to => 'welcome#index'

in routes.rb.

Upvotes: 5

Views: 5314

Answers (6)

Douglas G. Allen
Douglas G. Allen

Reputation: 2261

Keep this if you want it to be your context root after you generate your welcome parts.

Rails.application.routes.draw do
  root 'welcome#index'
end

Upvotes: 0

cchapman
cchapman

Reputation: 3377

I'm very very new to Rails and also ran into this error while following along with Rails Tutorial by Michael Hartl. The problem I had was that in the config/routes.rb file, I just uncommented the root :to => "welcome#index":

# just remember to delete public/index.html.
root :to => "welcome#index"

but with the structure of the sample_app was that "welcome#index" should be 'pages#home' instead, since everything was originally set up through the "pages" controller.

root :to => 'pages#home'

It's even right there in the book, but I just overlooked it and spent quite a while afterwards trying to figure out where I went wrong.

Upvotes: 5

lee
lee

Reputation: 8115

If you not generate the page with name welcome, then just generate the page like: $ rails generate controller pagename index. So then into the: config->routes.rb you should edit root 'welcome#index' to root 'pagename#index'

Upvotes: 1

Bruno
Bruno

Reputation: 6469

rails generate controller welcome index

Upvotes: 1

mu is too short
mu is too short

Reputation: 434965

When you say

root :to => 'welcome#index'

you're telling Rails to send all requests for / to the index method in WelcomeController. The error message is telling you that you didn't create your WelcomeController class. You should have something like this:

class WelcomeController < ApplicationController
  def index
    # whatever your controller needs to do...
  end
end

in app/controllers/welcome_controller.rb.

Upvotes: 12

randomguy
randomguy

Reputation: 12252

Make sure WelcomeController is defined in a file called welcome_controller.rb

Upvotes: 1

Related Questions