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.
Related
When I hit this link:
link_to("Accept", invitation_sumbit_invitation_url(invitations), method: :put)
I get this error:
uninitialized constant InvitationsController
The method in InvitationsController looks like this:
def sumbit_invitation
#invi = #invitations.find(params[:id])
#invi.update_attributes(accepted: true)
end
and routes is:
resources :invitations do
put :sumbit_invitation
end
I propose you can follow the below steps to resolve your problem:
1/ run command rake routes to make sure your routes are correct?
In my opinion, it should be:
resources :invitations do
member do
put :sumbit_invitation
end
end
2/ Make sure you URL is correct
link_to("Accept", invitation_sumbit_invitation_url(invitations), method: :put)
should be changed to
link_to("Accept", invitations_sumbit_invitation_path(invitations), method: :put)
Using rake routes to get correct path
4/ Make sure that your controller InvitationsController worked
P/S: It is better if you share your routes.rb file or logs rake routes
You controller is in the module ControlPanel and lives in the control_panel subfolder of your app's controller's folder, but your route doesn't have a corresponding controll_panel namespace defined.
Add a controll_panel namespace to your routes:
namespace :controll_panel do
resources :invitations do
put :sumbit_invitation
end
end
This will make the invitation_sumbit_invitation_url invalid and you will have to change it to something like admin_invitation_sumbit_invitation_url or invitation_sumbit_admin_invitation_url. From the top of my head I am not sure how Rails handles the naming. Use rake routes in your console to get a list of all available route names in your app.
I this context I advise to read: The Rails Guide: Routing
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
This problem has taken all day of mine...
Well;
I'm just trying to put all of my administration pages inside an /admin directory and to receive them via domain/admin style only. I've tried to make it run with this guide.
According to that official guide, what I'm looking for is using scope in my routes.rb file. BECAUSE, I have used named routes tones of times inside my pages. I do not want my program_path named route to change admin_program_path since I have 28 different usage of it.
So I'm supposed to use scope instead of namespace.
Issue is: I can not make scope work with my project.
Here is my routes.rb
scope "/admin" do
get "access/login"
get "access/index"
match "access/login_attempt", to: "access#login_attempt"
match "access/logout", to: "access#logout"
resources :admin_users
root to: 'programs#index'
resources :programs
resources :program_categories
resources :program_subcategories
resources :articles
resources :pictures
match '/kategoriler/:id' => 'program_categories#show'
match '/kategoriler' => 'program_categories#index'
match '/kategori/yeni' => 'program_categories#new'
match 'program/yeni' => 'programs#new'
match 'programlar' => 'programs#index'
match '/progam_categories/select_category/:program_id' => 'program_categories#select_category'
match '/program_subcategories/select_subcategory' => 'program_subcategories#select_subcategory'
match '/program_subcategory/add_subcategory' => 'program_subcategories#add_subcategory'
end
Here is my controller beginning :
class ProgramsController < ApplicationController
Just like told here:
If you want to route /admin/posts to PostsController (without the Admin:: module prefix), you could use
scope "/admin" do
resources :posts, :comments
end
As a result, what am I getting?
This error message:
Routing Error
uninitialized constant ProgramsController
Whichever controller I try to access, error changes that way.. Such like uninitialized constant ProgramCategoriesController , uninitialized constant ProgramSubcategoriesController etc...
I've tried to place application_controller both inside admin folder and root of controllers directory... No way.
Where is my mistake here? :(
Thanks in advance...
Try with :module parameter:
scope '/admin', :module => 'admin' do
# ...
end
The assumption is that your controllers are in Admin module namespace, so they start with 'Admin::'.
[EDIT]
It is response to your problem in comments below about path conflicts. You can use :as parameter, for example:
scope '/admin', :module => 'admin', :as => 'admin' do
# ...
end
You can check it with rake routes. All routes in the admin scope should now begin with 'admin_'
weird, i know but using user_root_path in production does not work. When i click on the link myapp.com/user i get a 404 page.
The log file doesn't show spit but a failed attempt:
Started GET "/user" for 123.125.146.23 at 2011-01-19 19:40:45 +0000
ActionController::RoutingError (uninitialized constant User::UsersController):
Now the only way to see something about this unitialized constant is to turn on rails c and type the constant into the console. Here is what happens:
ruby-1.9.2-p136 :005 > User::UsersController
(irb):5: warning: toplevel constant UsersController referenced by User::UsersController
=> UsersController
Now some digging found that this toplevel warning could be messing with it. But the log says bubkiss.
So i changed the route file from:
devise_for :users
namespace :user do
root :to => "users#index"
end
resources :subdomains
match '/user' => 'users#index'
to:
devise_for :users
namespace :user do
root :to => "subdomains#index"
end
resources :subdomains
match '/user' => 'users#index', :controller => :users
The thought was that maybe production environment did not like a user#index... so i changed it to subdomains#index. I can get /subdomains no problem. so the actual page will show, it's the route that is fudged... any thoughts?
setup: rails 3.0.3, devise 1.1.5 (and was 1.1.3 upgraded, same problem)
I used
devise_for :users do
match 'user' => "users#index", :as => :user_root, :constraints => { :domain => SITE_DOMAIN}
end
In each of your development.rb or production.rb files you would have a SITE_DOMAIN constant so like:
::SITE_DOMAIN = "lvh.me"
#in development.rb I was using subdomains with the helpful lvh.me google it.
or in production.rb
::SITE_DOMAIN = "mydomain.com"
Again i needed subdomains, so this worked for me.
The devise wiki did not work for me. Once i have time i will update that too, or submit a ticket, but this is just google juice for those that need it.
If you are namespacing your routes, you need to namespace your controllers as well.
Move controllers/users_controller.rb to controllers/user/users_controller.rb and edit it to add in the module:
class User::UsersController < ApplicationController
end
But my guess is you aren't actually meaning to use namespace in the route.
I had the same problem with /user giving a 404 in production. Here is the solution I ended up with which I think is simpler than messing with the routes. In ApplicationController put:
def after_sign_in_path_for(resource)
stored_location_for(:user) || landing_welcome_path
end
Can someone explain how the environment affects routing in rails 3?
I have an app that has the following in the routes file:
namespace "admin" do
# ADMINISTRATIVE ROUTES ONLY
root :to => 'home#index'
resources :comments do
member do
get :approve
get :reject
end
end
resources :users do
member do
get :block
get :unblock
end
end
end
When browing to /admin locally, I am greeted by the appropriate page.
On the same URL on the heroku version I get a 404. The route shows
correctly in 'heroku rake routes'
Logs are showing:
ActionController::RoutingError (wrong constant name Admin/
homeController):
/disk1/home/slugs/196384_c95a9e3_4463/mnt/.bundle/gems/gems/
activesupport-3.0.0.beta4/lib/active_support/inflector/methods.rb:
103:in `const_defined?'
Any ideas?
It transpires that this is an issue with the right_aws gem that we had in our Gemfile.
It looks like it's finding a lowercase "homeController" class, but I don't understand why. Check your file to make sure you've named your class correctly?
Looks like a bug in the ActiveSupport beta. Are you running exactly the same rails version?