簡體   English   中英

Rails 3-同一控制器用於兩個范圍不同的子域

[英]Rails 3 - Same controller for two subdomains different scopes

我想對2個子域使用相同的控制器,但作用域不同。

例如(在我的routes.rb中):

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

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

現在,當我運行“耙子路線”時,我同時擁有“ / mystuff / photos”和“ / photos”。

我有兩個問題:

  1. 這樣可以嗎?
  2. 在這種情況下,如何使用命名路由? 我有類似admin_photos_url和www_photos_url的東西嗎?

我認為這樣做很好... 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"}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM