Subdomains, Session Data, & Routing

1 view
Skip to first unread message

Clint Pachl

unread,
Oct 31, 2006, 7:12:35 AM10/31/06
to rubyonra...@googlegroups.com
SCENARIO
My app has two types of accounts, personal and business. Both accounts
will have some similar controller/action names, however, they will
behave differently. Additionally, the business account will have many
more controllers not found in the personal account.

QUESTION
How do I partition my app so that I can isolate personal and business
controllers under under a single or dual subdomain configuration?

OPTION 1
Create two subdomains for each account, personal.domain.com and
business.domain.com. If I take this approach, I see trouble down the
road when other parts of the app need to link back to the user's
account. At some point a decision will have to be made, is it
personal.domain.com or business.domain.com? If possible, I was thinking
that routes could be selected, in routes.rb, based on the session data,
(e.g. check for personal or business account)?

OPTION 2
Create a single account domain, my.domain.com, and then select the
correct controller/action for the user's account in the
ApplicationController. For example, in routes.rb (request routing plugin
installed):

map.with_options(:condition => {:subdomain => 'my'}) do |x|
x.connect '*path',
:controller => 'application',
:action => 'route_to_account'
end

Then the ApplicationController::route_to_account will handle the
controller/action routing to the personal or business controller/action
contingent upon session data. If I take this approach, it seems I loose
all the cool routing stuff, like named routes, and url variables in
@params?

What should I do? Any advice is greatly appreciated.

-pachl

--
Posted via http://www.ruby-forum.com/.

Starr

unread,
Oct 31, 2006, 10:52:36 AM10/31/06
to Ruby on Rails: Talk
Option #2 definately. #1 would be full of headaches. You could just map
'account/personal' and 'account/business' to the different controllers.
I don't see why you would loose any routing abilities.

Starr

pachl

unread,
Oct 31, 2006, 6:46:10 PM10/31/06
to Ruby on Rails: Talk
On Oct 31, 8:52 am, "Starr" <snho...@gmail.com> wrote:
> Option #2 definately. #1 would be full of headaches. You could just map
> 'account/personal' and 'account/business' to the different controllers.

So my next question, where exactly would I map my controller, in
routes.rb or ApplicationController (as I mention in Option#2)?

When the request comes into the routing code, my only way of
determining if a user is a business or personal account is via
sessions[:business_id] (it will be nil for personal and set for
business). So how could I get at this session variable? Is there
another way of doing this?

> I don't see why you would loose any routing abilities.

If I take the option#2 approach, I wouldn't be able to setup named
routes like the following:

map.with_options(:condition => {:subdomain => 'my'}) do |x|

if session[:business_id]
# business account mappings ...
biz.connect ':action/:id', :controller => 'account/business'
else
# personal account mappings ...
per.connect ':action/:id', :controller => 'account/personal'
end
end

I want to take approach #2, but not sure how to do this using a single
subdomain that maps to two backend controllers. My only differentiator
between the two accounts is the session variable "business_id".

Is there a way to get at session data in config/routes.rb? If so, I
think I can acheive option#2!

I'm very new at Rails so a good explaination will be greatly
appreciated. This is my first time using Rails; I am rewriting an PHP
app.

-pachl

Reply all
Reply to author
Forward
0 new messages