Rails的路线:定义根命名空间(Rails route: define root to names

2019-08-01 10:17发布

我有2个控制器:

app/
   /controllers
      posts_controllers.rb
      /mobile
         posts_controllers.rb

我的routes.rb看起来是这样的:

root :to => "posts#index"
resources :posts

namespace :mobile do
   root :to => "posts#index"
   resources :posts
end

但是,当我访问/mobile ,它反正渲染第一个控制器的索引页,也试过这样:

namespace :mobile do
   root :to => "mobile/posts#index"
   resources :posts
end

但它给我的错误: uninitialized constant Mobile::Mobile我想渲染第二控制器的索引页,我该怎么办呢?

编辑

通过输入/移动我想渲染设在这里的文件:

app/
   views/
       /mobile
          /posts
             index.html.erb

但它在这里撕心裂肺的文件:

app/
   views/
       /posts
          index.html.erb

Answer 1:

namespace :mobile do
   root :to => "posts#index"
   resources :posts
end

root :to => "posts#index"
resources :posts

代替

root :to => "posts#index"
resources :posts

namespace :mobile do
   root :to => "posts#index"
   resources :posts
end


Answer 2:

namespace :mobile do
   get "/" => "posts#index"
   resources :posts
end

使用命名空间已经预先考虑移动到该块内部控制器的名称。



Answer 3:

Rails.application.routes.draw do

 root to: 'front/homes#index'

 namespace :front do
  resources :homes
 end 

 namespace :admin do 
  resources :interviews 
 end

 resources :meetings do 
  resources :slides
 end

end

rails5它的工作原理。



文章来源: Rails route: define root to namespace