2012-10-12 12 views
5

Tôi đã có 2 bộ điều khiển:Rails tuyến đường: xác định gốc để namespace

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

và routes.rb của tôi trông như thế này:

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

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

nhưng khi tôi ghé thăm /mobile, nó nào render index trang của bộ điều khiển đầu tiên, cũng đã thử điều này:

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

nhưng nó cho tôi lỗi: uninitialized constant Mobile::Mobile Tôi muốn hiển thị trang chỉ mục của bộ điều khiển thứ hai, tôi có thể làm như thế nào?

Sửa

Bằng cách nhập/mobile tôi muốn làm cho các file nằm ở đây:

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

Nhưng nó xé file ở đây:

app/ 
    views/ 
     /posts 
      index.html.erb 
+0

Bạn đang làm tốt, chỉ cần kiểm tra xem lớp 'controllers/mobile/posts_controller.rb' giống như:' class Mobile :: PostsController jan267

Trả lời

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

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

thay vì

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

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

Đây là giải pháp tốt hơn nhiều so với câu trả lời được chấp nhận! – ReggieB

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

Sử dụng namespace đã prepends di động đến tên của bộ điều khiển bên trong khối đó.

+2

nó không hoạt động –

+1

lỗi là gì? bạn có thể cho tôi hoàn thành 'routes.rb' được không? – shime

+1

tuyến đường hoàn chỉnh.rb nằm trong câu hỏi –

1
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 

Trong rails5 nó hoạt động.