5

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.

1
  • 2
    I also got this error when using the getting started guide: guides.rubyonrails.org/getting_started.html You have to actually change the line to home#index like Mohan Raj says in his comment below. You don't just un-comment the line. Commented Dec 31, 2011 at 4:40

6 Answers 6

12

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi i found the solution just i changed root :to => 'home#index'. I created the controller home. So set route to home.
@MohanRaj you should accept this answer as correct, as it is.
5

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.

Comments

1

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

Comments

1

rails generate controller welcome index

1 Comment

The generator sets the route for you. Rails.application.routes.draw do get 'welcome/index' end
1

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'

Comments

0

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

1 Comment

Years late and thousands short. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.