Devise user_root_path gets 404'd in production

2019-06-28 04:04发布

问题:

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)

回答1:

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.



回答2:

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.



回答3:

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?