MKK
MKK

Reputation: 2753

How can I use sub-domain as parameter in Rails3?

When I access to http://testuser.example.com, I'd like it to access to 'user' controller's 'show' action with the parameter 'testuser', which is user name in user Model.

How can I archive this easily??

Upvotes: 2

Views: 816

Answers (1)

Alper Karapınar
Alper Karapınar

Reputation: 2694

This should work

@user = User.find_by_name(request.subdomain)

update

for routes

constraints(:subdomain => /.+/) do
  root :to => 'user#show'
end

simply you define new routing rules when subdomain exists, you can change it according to your needs.

Upvotes: 4

Related Questions