2012-01-01 11 views
2

2つのサブドメインに対して異なるスコープで同じコントローラを使用したいとします。例えばRails 3 - 2つのサブドメインの異なるスコープの同じコントローラ

(私のroutes.rbを中):私は "すくいルート" を実行すると

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

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

は、今私は "/のMyStuff /写真" と "/写真" の両方を持っています。私は2つの質問がある

  1. は、それがこれを行うにはOKですか?
  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"} 
関連する問題