【问题标题】:Bad routing or rails bug?路由错误或rails错误?
【发布时间】:2012-05-22 21:42:04
【问题描述】:

我已经将路由设置到命名空间中,所以它看起来像

root to: "home#index"

namespace :users do
  root to: "profile#index"
  resources :registrations
  resources :sessions
end

namespace :admin do
  root to: "base#index"
end

rake routes |grep root
                root          /                               home#index
          admin_root          /admin(.:format)                admin/base#index
          users_root          /users(.:format)                users/profile#index

在我的标题导航中,我有= link_to "home", root_path

在开发模式下一切正常,但在生产中完全崩溃

我在尝试访问会话/注册控制器(用户/会话/新)时收到 No route matches {:controller=>"users/home"}

我标头中的 root_path 尝试在 users 命名空间中获取 home 控制器

提前致谢

【问题讨论】:

  • 你能运行`bundle exec rake routes | grep root' 在生产中?

标签: ruby-on-rails routes dev-to-production


【解决方案1】:

用户命名空间中没有主控制器,用户命名空间中有配置文件控制器。

您需要 users_root_path 才能访问“users/profile#index”。

但你是对的,我希望 root_path 会转到“home#index”。

【讨论】:

    【解决方案2】:

    区分每个的根路径,然后像这样尝试

    root to: "home#index" , :as => home_root
    
    namespace :users do
      root to: "profile#index" , :as => users_root
      resources :registrations
      resources :sessions
    end
    
    namespace :admin do
      root to: "base#index" , :as => admin_root
    end
    

    使用路径如:home_root_path、users_root_path、admin_root_path

    【讨论】:

      猜你喜欢
      • 2010-12-26
      • 2018-06-02
      • 2012-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多