Routing error in Ruby on Rails 3
Asked Answered
C

6

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.

Carine answered 24/9, 2011 at 6:25 Comment(1)
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.Blower
T
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.

Thrift answered 24/9, 2011 at 6:32 Comment(2)
Hi i found the solution just i changed root :to => 'home#index'. I created the controller home. So set route to home.Carine
@MohanRaj you should accept this answer as correct, as it is.Yun
C
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.

Cohberg answered 20/11, 2012 at 17:27 Comment(0)
L
1

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

Liggins answered 24/9, 2011 at 6:30 Comment(0)
A
1

rails generate controller welcome index

Ashram answered 28/10, 2013 at 23:52 Comment(1)
The generator sets the route for you. Rails.application.routes.draw do get 'welcome/index' endBailiff
R
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'

Recliner answered 20/5, 2014 at 3:34 Comment(0)
B
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
Bailiff answered 12/6, 2017 at 17:55 Comment(1)
Years late and thousands short. :-)Bailiff

© 2022 - 2024 — McMap. All rights reserved.