Rails route: define root to namespace

☆樱花仙子☆ 提交于 2019-12-07 00:23:06

问题


I've got 2 controllers:

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

and my routes.rb looks like this:

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

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

but when i visit /mobile, it's anyway rendering index page of first controller, also tried this:

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

but it's giving me error: uninitialized constant Mobile::Mobile I want to render the index page of second controller, how can i do that ?

Edit

By entering /mobile i want to render files located here:

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

But it's rending files here:

app/
   views/
       /posts
          index.html.erb

回答1:


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

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

instead of

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

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



回答2:


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

Using namespace already prepends mobile to the names of controllers inside that block.




回答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

In rails5 it works.



来源:https://stackoverflow.com/questions/12857996/rails-route-define-root-to-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!