8

我有 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 我想渲染第二个控制器的索引页面,我该怎么做?

编辑

通过输入 /mobile 我想呈现位于此处的文件:

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

但它在这里渲染文件:

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

3 回答 3

24
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
于 2014-06-08T17:09:02.733 回答
8
namespace :mobile do
   get "/" => "posts#index"
   resources :posts
end

使用命名空间已经将mobile添加到该块内的控制器名称之前。

于 2012-10-12T11:31:46.827 回答
5
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 中它可以工作。

于 2017-02-23T11:45:18.040 回答