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.
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.
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
.
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.
Make sure WelcomeController is defined in a file called welcome_controller.rb
rails generate controller welcome index
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'
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
© 2022 - 2024 — McMap. All rights reserved.