I've received error "uninitialized constant ContactsController"? - ruby-on-rails

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

Related

How do I create a new path/route in Ruby and then link to it?

I have done it before but am having trouble adding a new page and a new path to my rails server.
Pretty much, I want to add a new page and then link to that page in a drop down menu... but I am having trouble getting the changes to take effect and for the new path/route to show up when I do "rails routes".
I have done it before for an "offerings" page at pages#offerings but can't seem to figure out how to repeat the same process
I started off going to the pages controller and adding a "def public_speaking" and "end":
Pages Controller
# GET request for / which is our home page
def home
#basic_plan = Plan.find(1)
#pro_plan = Plan.find(2)
end
def about
end
def offerings
end
def public_speaking
end
end
Routes.rb
Then in Routes.rb I tried using the same process (Adding get 'public_speaking', to : 'pages#public_speaking')
root to: "pages#home"
devise_for :users, controllers: { registrations: 'users/registrations' }
resources :users do
resource :profile
end
get 'about', to: 'pages#about'
resources :contacts, only: [:create]
get 'contact-us', to: 'contacts#new', as: 'new_contact'
get 'offerings', to: 'pages#offerings'
get 'public_speaking', 'pages#public_speaking'
end
View file
I also created a file "public_speaking.html.erb" in the views folder with the same name.
What am I doing wrong/missing to create this new path? Is there some command to execute this linkage or something?
I expected there to be a new route created (since it worked for "offerings"), however it has not worked and I'm not sure why. I will be repeating this process for 5-6 pages, so I want to be sure I can do it right
i see in your routes, it seems your code is not correct.
you should change:
from get 'public_speaking', 'pages#public_speaking'
to get 'public_speaking', to: 'pages#public_speaking'
Khan Pham has given a correct answer. Seems you are messing with link.
Accordingly to Ruby on rails guide the proper route would be:
get 'public_speaking', to: 'pages#public_speaking'
where to: expects controller#action format.
And then you can check your routes by executing command rake routes and if your part presents there you can use it in your views like:
link_to('Public Speaking', public_speaking_path)
you can read more about url here. Good luck!

rails route error undefined local variable or method

I'm a beginner of RoR and this is my sample
class PagesController < ApplicationController
def welcome
end
end
in my route file
Rails.application.routes.draw do
root :to => “pages#welcome”
...
end
when I open powder
#config/routes.rb
root to: "pages#welcome"

Routing error - uninitialized constant

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.

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.
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

How to fix this up? " Unknown action The action 'index' could not be found for UsersController"

When I go to my localhost:3000/users page, I get:
Unknown action
The action 'index' could not be found for UsersController.
If you were following Hartl's tutorial,then accessing localhost:3000/users will cause this error. Try localhost:3000/signup instead.
You need not define the default actions (assuming appropriate http method), all you need to do is add the following to your config/routes.rb
resources :users
First you need to make sure that your controller actually has an index action, so
class UsersController < ApplicationController has to include the def index ... end in it.
Also, make sure your routes are set up correctly using
resources :users
and check it by typing
rake routes
in the terminal to check that the routes are right.
You might also want to check that the root is set up correctly in the config/routes.rb file
if you got the same problem with me (I cant access the localhost:3000/users but I can access my localhost:3000/signup), it might be works for u.
First, in your users_controller.rb (Controller for Users) , add
def index
end
Then , make a file "index/html/erb" in your app/views/users/index.html.erb
and put this code
<% controller.redirect_to "/signup" %>
You might have to rerun your server, and it works on my problem.
Remember, if you've included
get 'signup' => 'users#new
resources :users
…in your routes.rb file, then you need to use localhost:3000/signup instead. I believe if you removed get 'signup' => 'users#new and left only resources :users then using localhost:3000/users would take you to the new user signup form.
Remove the debugger line. Also, make sure you have exit the rails console.
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
debugger
end
def new
end
end

Resources