Loupax
Loupax

Reputation: 4914

How can I set route with a specific subdomain in Rails 3?

I am trying to set two different routes in rails, each linked to a different subdomain

This is something like I want do do

match "/"  => "first_app#index",
              as => :first_app_root, 
              :subdomain => 'application' 

match "/"  => "second_app#index",
              :as => :second_app_root,  
              :subdomain => 'another_application' 

The result I want to get of course is application.my_website to point to the index action of the FirstAppController and the another_application.my_website.dev to point to the index action of the SecondAppController.

Also the first_app_root_url and second_app_root_url helper functions should create urls complete with the proper subdomains

Is that possible?

Upvotes: 0

Views: 92

Answers (1)

Arkan
Arkan

Reputation: 6236

If you want to do this with match, you should be able to do it with the following statement.

match "/" => "first_app#index", :constraints => {:subdomain => "application"}
match "/" => "second_app#index", :constraints => {:subdomain => "another_application"}

but a cleaner way would probably be

constraints :subdomain => "application" do
  # add your routes normally
end

constraints :subdomain => "another_application" do
  # add your routes normally
end

Documentation: http://guides.rubyonrails.org/routing.html#segment-constraints

Upvotes: 1

Related Questions