Rails - Failing Routes in deployment - ruby-on-rails

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?

Related

Rails uninitialized constant error with boolean variable

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

Rails namespace root error

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

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 on Heroku (after logging in via Devise)

For my rails 3 app I have a route setup as follows
namespace :user do
root :to => "reading_schedules#index"
end
This is what my "rake routes" shows
user_root /user(.:format) {:controller=>"user/reading_schedules", :action=> "index }
Everything works fine on my localmachine. But as soon as I push the site up to Heroku and login I get the following error in my logs
ActionController::RoutingError (uninitialized constant User::ReadingSchedulesController):
If I navigate to the root of the site everything else works fine. But this one url doesn't work. The url it's trying to hit is website/user but like I said, it works fine on my localmachine.
EDIT:
Here's the rest of my routes file
devise_for :users, :path => 'accounts'
root :to => "home#landing"
namespace :user do
root :to => "reading_schedules#index"
end
resources :users do
resources :reading_schedules
member do
get :change_password
post :change_password
end
end
resources :reading_schedules do
member do
get :recalculate
end
end
I found this question first in looking for an answer to the same issue. For any future searchers, check out this link.
For me, it was a combination of the last two answers in this post (adapted for my controllers of course.)
Also of note, this corrected the issue without having to reset the database.
ActionController::RoutingError (uninitialized constant User::UsersController) in heroku (but everything works in local)
Probably not the answer you are looking for but
heroku rake db:reset
solved the problem for me. I didn't have any critical data in the
db so it was not a problem.

Devise user_root_path gets 404'd in production but not dev?

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?

Resources