Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 568
Said Kaldybaev Avatar asked Oct 12 '12 11:10

Said Kaldybaev


2 Answers

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
like image 141
mollyjane Avatar answered Oct 03 '22 17:10

mollyjane


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

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

like image 44
shime Avatar answered Oct 03 '22 17:10

shime