User specific data in subdomains

Hi,

I have an app with specific data to each user. I want user george to
be able to access his data at george.domain.com, user dan to access
his data at dan.domain.com and so on. So I’d have a Users controller
and I suppose I’d have to map domain.com/users/show/george to
george.domain.com. How can I do this? Do I use Request Routing plugin?
Though, how do I do it with this plugin also?

Thanks ;).

On Nov 12, 2007, at 7:01 AM, [email protected] wrote:

I have an app with specific data to each user. I want user george to
be able to access his data at george.domain.com, user dan to access
his data at dan.domain.com and so on. So I’d have a Users controller
and I suppose I’d have to map domain.com/users/show/george to
george.domain.com. How can I do this? Do I use Request Routing plugin?
Though, how do I do it with this plugin also?

Here’s how I do it:

I create a class which contains all the logic needed to dictate the
behaviors of a subdomain. Let’s call this class Subdomain (usually it
would be an ActiveRecord class). This class will typically just
initialize a bunch of instance vars used as settings that make a
Subdomain unique.

One of those settings is going to be one or more query filters to
restrict data access. These are just SQL fragments (or :conditions
hashes) which will modify the WHERE clause of all queries to restrict
the query to the conditions suitable to the subdomain. So, in your
case we might have “AND subdomain_name = ‘george’” as the query filter.

class Subdomain

attr_reader :site_name, :query_filter

def initialize
@query_filter
end

def load_settings
… does a query to get all settings…
… load results into instance vars…
end

end

Then early in application.rb, you would create an object something
like this:

@current_subdomain = Subdomain.new
@current_subdomain.load_settings(self.request.subdomains.join(‘.’))

You may or may not need the join part (I do state and county
subdomains for some stuff, and that will join a multiple subdomin
list into a single string).

Now for all the queries which collect data, you can use the value(s)
in @current_subdomain.query_filter to add to the :conditions or
modify an SQL string to restrict the query to specific records
pertaining to the subdomain.

– gw (www.railsdev.ws)

On Nov 12, 2007, at 1:29 PM, [email protected] wrote:

Greg, thanks for your reply. I understand how to work with this, but
how do I set the usage of subdomains in routes.rb? do I need to do
anything special? you didn’t mention if you use any plugin for this?

There’s nothing to do in routes. No plugins. It’s just simple
application logic that populates an object by reading the subdomain
from the HTTP headers – which is what request.subdomains gives you.
Then using you own object’s instance vars where you need them. No
magic needed.

– gw (www.railsdev.ws)

Hi Greg,

I din’t get how one can map request URL from
domain.com/users/show/george
To
george.domain.com

which indirectly means that when the user in the browser types
domain.com/users/show/george”, how i can
take that request to “george.domain.com”. And one more thing is
since “george” is a dynamic value, i am not able
to understand how to do it.

Please make me understand,

Thanks,

Regards,
Raghu Kumar K

Greg, thanks for your reply. I understand how to work with this, but
how do I set the usage of subdomains in routes.rb? do I need to do
anything special? you didn’t mention if you use any plugin for this?

On Nov 13, 2007, at 7:37 AM, raghukumar wrote:

I din’t get how one can map request URL from
domain.com/users/show/george
To
george.domain.com
which indirectly means that when the user in the browser types
domain.com/users/show/george”, how i can
take that request to “george.domain.com”.
And one more thing is since “george” is a dynamic value, i am not able
to understand how to do it.

I went ahead and wrote up an article on all this:

http://www.railsdev.ws/blog/10/using-subdomains-in-rails-apps/

– gw (www.railsdev.ws)

Then using you own object’s instance vars where you need them. No
magic needed.

On Nov 13, 2007, at 7:37 AM, raghukumar wrote:

I din’t get how one can map request URL from
domain.com/users/show/george
To
george.domain.com
which indirectly means that when the user in the browser types
domain.com/users/show/george”, how i can
take that request to “george.domain.com”.

No, that wasn’t the original question. We’re not trying convert URLs
from one format to another. The original question was how to use the
explicit URLs of george.domain.com, dan.domain.com, mary.domain.com,
and any *.domain.com and have Rails figure out what to do based on
that (which turns out to be quite simple).

And one more thing is since “george” is a dynamic value, i am not able
to understand how to do it.

Simply configure your HTTP server to accept any subdomain. In Apache
this would be:

ServerName domain.com
ServerAlias *.domain.com

Now Apache will accept every subdomain.

If someone submits nonsense.domain.com and you have no data for
“nonsense,” you serve a default page that tells the user that. It’s
just a “page not found” kind of thing.

– gw (www.railsdev.ws)