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
Related
I'm trying to create a form page, but keep getting the issue when I try and run the program. Using Ruby On Rails.
routes.rb is:
Rails.application.routes.draw do
root to: 'pages#home'
get 'about', to: 'pages#about'
get 'signup', to: 'pages#signup'
resources :contacts
end
contacts_controller is:
class ContactsController < ApplicationController
def new
end
end
What's the issue?
So normally if you get that error, it means you have a typo either in the file name or the class name at the top of the file.
So try to check that.
Make sure that the file is not misspelled. Should just be contacts_controller.rb
I'm trying to learn Ruby on Rails, from here: http://guides.rubyonrails.org/getting_started.html , But when I try to set the root for the route up in Section 4.3, although the route is recognized, when I run the server and localhost:3000, it still thinks there's no root route. What could the problem be? I'm using Ruby 1.9.3.
Routes
Without your routes file, or even the error which is being presented, I'll just give you what you need to get the "root" route working:
#config/routes.rb
root "application#index" #-> syntax - "controller#action"
This has to be backed up with the appropriate controller action:
#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
def index
end
end
It also needs the appropriate views:
#app/views/application/index.html.erb
This is the root page!
I'm running into a couple of errors when i try to define a root to a namespace. To recreate this, I'll rebuild a project from scratch.
rails new rails_test
cd rails_test
rails generate controller admin
rake db:migrate
Now I put some boilerplate in app/controllers/admin_controller.rb
class AdminController < ApplicationController
def index
end
end
in app/views/admin/index.html.erb
<p>Index</p>
and finally in config/routes.rb
root to: 'admin#index'
This all works perfectly, when i start the server and hit '/' (root) url (it goes to the admin index page), but it's not what I want.
This is meant to be part of a bigger project and I want to hit the /admin url and get the admin index so (following from http://guides.rubyonrails.org/routing.html#using-root) I change routes.rb to:
namespace :admin do
root to: "admin#index"
end
but I get a routing error:
uninitialized constant Admin
with a routes list containing 1 line:
admin_root_path GET /admin(.:format) admin/admin#index
My thinking from reading the end of this last line is that I'm already in the admin namespace so maybe i don't need to specify the controller index is in so I try changing routes to:
namespace :admin do
root to: "index"
end
But that gives me an ArgumentError saying "missing :action" on the 'root to: "index"' line.
I can get around it by using scope, but it looks like using namespace is a bit cleaner and I want to understand whats going wrong here.
Ruby/Rails versions
ruby -v -> ruby 1.9.3p392 (2013-02-22) [i386-mingw32]
rails -v -> Rails 4.0.0
Any help is apprechiated
namespace namespaces controllers as well. Change yours to
class Admin::AdminController < ApplicationController
def index
end
end
And move it under app/controllers/admin. That's what admin/admin#index means. :)
According to rubyonrails.org guide on routing you should be able to do something like this.
it should looks like this
namespace :admin do
root to: "admin#index"
end
root to: "home#index"
What do you get with rake routes?
on rails 4 in routes two roots not permited now
use
get "/admin" => "admin/admin#index", :as => "admin"
Type in terminal
rails generate scaffold Admin::User username email
rake db:migrate
if only Controller type this
rails generate controller Admin::User
I think what you really want is something like
namespace :admin do
get 'other'
end
get 'admin' => 'admin#index'
This allows for the index and other methods to be in straightforward AdminController controller in usual directory, views to go in views/admin etc.
You could probably also use
resource :admin, only: [:index] do
get 'other'
end
I could not fix this in Rails 3.2.12, maybe I am missing something.
config/routes.rb
get "home/index"
root :to => "home#index"
devise_for :users, :only => :omniauth_callbacks
match 'users/auth/:provider/callback' => 'authentications#create'
match '/auth/:provider/signout' => 'authentications#signout'
app/controllers/authentication_controller.rb
class AuthenticationsController < ApplicationController
...
end
app/models/authentication.rb
class Authentication < ActiveRecord::Base
...
end
I think it should work with my current knowledge, but there is something that I miss.
My kind question would be to tell what is wrong, please.
Rounting Error
uninitialized constant AuthenticationsController
This is a message that shows up at http://localhost:3000/auth/facebook/signout
Rails requires the file name to match the class name. Therefore you should rename app/controllers/authentication_controller.rb to app/controllers/authentications_controller.rb.
Though this question has been answered, I found another case where I was getting this error and wanted to document it here for posterity.
If you have two similar routes defined in your routes.rb file without the corresponding controllers you will get the uninitialized constant error.
Steps to reproduce:
rails generate scaffold foobar name:string
bundle exec rake db:migrate
add resources :foobars to routes.rb to a new scope (note: the foobars resource was already automatically added to the top of your routes.rb during scaffold generation) like this:
resources :foobars
########################################
# SUPER
########################################
constraints host: ENV['SUPER_HOST'] do
scope module: :super do
resources :foobars
get '/' => 'super#index'
end
end
Now, move /app/views/foobars to /app/views/super/foobars
and, move /app/controllers/foobars_controller.rb to /app/controllers/super/foobars_controller.rb
Make sure foobars_controller.rb is in the Super module:
class Super::FoobarsController < ApplicationController
Now go to your.dev.server/foobars/
You should get this error:
Routing Error uninitialized constant FoobarsController
Now, remove resources :foobars from beginning of routes.rb
It should work now!
It took me awhile to figure out why I was getting this error, and I didn't realize that generating the scaffold adds an entry in routes.rb
While it doesn't answer your specific question, I received the failure with the following in my routes.rb
resources :republishes do
post '/attempt_all', :to => 'republishes/#attempt_all' . . .
which I changed to
resources :republishes do
post '/attempt_all', :to => 'republishes#attempt_all' . . .
Removing the slash fixed my issue.
In my case, Since I'd scaffold the module, it was already had initiated routes for the controller and I was defining it twice. So by removing one of the duplicate resource routes resolved my issue.
make sure you've created your model for the controller in question.
I'm going to spend some of my easter vacation learning ruby on rails. I've encountered a problem with my routing. I hope you can help me with this.
What I'm trying to do is set my controller index in namespace home as the root (the controller I want to use when I hit the root of my website). Note that my controller is called index and the method I want to use is also called index.
Here is the structure of my controller(s):
app
-controllers
-home
-index_controller.rb
My index_controller.rb looks like this:
class Home::IndexController < ApplicationController
def index
#testing = 1
end
end
My routes.rb file looks like this:
MyFirstRail::Application.routes.draw do
namespace :home do
get "/" => "index#index"
end
end
I had a look at this question - but I couldn't make it work.
I'm using rails 3 and Rubymine as IDE (if it's any help).
This is how I do it in my project:
root :to => "home::index#index"
The structure is always the same with root :to (which is what is used to define the root route :))
root :to => "controller_name#action"
Your namespaced controller here is simply named home::index.
You can just try doing like this in routes,
root :to => "controller#action"