2012-01-01 49 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"} 
相關問題