2012-01-01 40 views
2

我想为两个子域使用相同的控制器,但具有不同的作用域。Rails 3 - 同一个控制器,用于两个子域不同的作用域

例如(在我的routes.rb):

constraints :subdomain => "admin" do 
    resources :photos 
end 

constraints :subdomain => "www" do 
    scope "/mystuff" 
    resources :photos 
    end 
end 

现在,当我运行 “耙路线” 都在我 “/的MyStuff /照片” 和 “/照片”。

我有2个问题:

  1. 是否确定要这样做吗?
  2. 在这种情况下,我该如何使用命名路线?我有类似admin_photos_url和www_photos_url的内容吗?

回答

4

我认为这样做很好...... Rails中的路由是灵活的原因(允许这样的情况)。

不过,我会改变你的路线更是这样为了正确命名您的路径助手:

scope :admin, :as => :admin, :constraints => { :subdomain => "admin" } do 
    resources :photos 
end 

scope '/mystuff', :as => :mystuff, :constraints => { :subdomain => "www" } do 
    resources :photos 
end 

,这将给你:

 admin_photos GET /photos(.:format)      {:subdomain=>"admin", :action=>"index", :controller=>"photos"} 
         POST /photos(.:format)      {:subdomain=>"admin", :action=>"create", :controller=>"photos"} 
    new_admin_photo GET /photos/new(.:format)     {:subdomain=>"admin", :action=>"new", :controller=>"photos"} 
    edit_admin_photo GET /photos/:id/edit(.:format)    {:subdomain=>"admin", :action=>"edit", :controller=>"photos"} 
     admin_photo GET /photos/:id(.:format)     {:subdomain=>"admin", :action=>"show", :controller=>"photos"} 
         PUT /photos/:id(.:format)     {:subdomain=>"admin", :action=>"update", :controller=>"photos"} 
         DELETE /photos/:id(.:format)     {:subdomain=>"admin", :action=>"destroy", :controller=>"photos"} 
    mystuff_photos GET /mystuff/photos(.:format)    {:subdomain=>"www", :action=>"index", :controller=>"photos"} 
         POST /mystuff/photos(.:format)    {:subdomain=>"www", :action=>"create", :controller=>"photos"} 
new_mystuff_photo GET /mystuff/photos/new(.:format)   {:subdomain=>"www", :action=>"new", :controller=>"photos"} 
edit_mystuff_photo GET /mystuff/photos/:id/edit(.:format)  {:subdomain=>"www", :action=>"edit", :controller=>"photos"} 
    mystuff_photo GET /mystuff/photos/:id(.:format)   {:subdomain=>"www", :action=>"show", :controller=>"photos"} 
         PUT /mystuff/photos/:id(.:format)   {:subdomain=>"www", :action=>"update", :controller=>"photos"} 
         DELETE /mystuff/photos/:id(.:format)   {:subdomain=>"www", :action=>"destroy", :controller=>"photos"} 
相关问题