Search code examples
ruby-on-railsrubyrails-routing

Rails route: define root to namespace


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

Solution

  • 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