# Parse request URL

**URL:** <https://www.ruby-forum.com/t/parse-request-url/96516>\
**Category:** Rails\
**Created:** [January 24, 2006, 11:12am UTC](https://www.ruby-forum.com/t/parse-request-url/96516 "2006-01-24T11:12:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [January 24, 2006, 11:12am UTC](https://www.ruby-forum.com/t/parse-request-url/96516/1 "2006-01-24T11:12:00Z")

</div>

Hello all.

Due to the mistakes of the company before I joined 😉 I am stuck with a  
multiple database setup. I am planning on parsing the REQUEST\_URL to  
decide which controller is being accessed and basing the  
establish\_connection on that. Before I go messing around with regexps I  
was wondering if rails has a built in function for parsing URLS. I have  
googled around a bit and seen net/http but I cannot find an API for it.

Thanks

Jeff

---

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [January 24, 2006, 11:18am UTC](https://www.ruby-forum.com/t/parse-request-url/96516/2 "2006-01-24T11:18:30Z")

</div>

Jeff J. wrote:

> Hello all.
> 
> Due to the mistakes of the company before I joined 😉 I am stuck with a  
> multiple database setup. I am planning on parsing the REQUEST\_URL to  
> decide which controller is being accessed and basing the  
> establish\_connection on that. Before I go messing around with regexps I  
> was wondering if rails has a built in function for parsing URLS. I have  
> googled around a bit and seen net/http but I cannot find an API for it.

The request URL is chopped up by config/routes.rb. I’d start there.

---

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [January 24, 2006, 11:55am UTC](https://www.ruby-forum.com/t/parse-request-url/96516/3 "2006-01-24T11:55:32Z")

</div>

Alex Y. wrote:

> Jeff J. wrote:
> 
> > Hello all.
> > 
> > Due to the mistakes of the company before I joined 😉 I am stuck with a  
> > multiple database setup. I am planning on parsing the REQUEST\_URL to  
> > decide which controller is being accessed and basing the  
> > establish\_connection on that. Before I go messing around with regexps I  
> > was wondering if rails has a built in function for parsing URLS. I have  
> > googled around a bit and seen net/http but I cannot find an API for it.
> 
> The request URL is chopped up by config/routes.rb. I’d start there.

I found out about require ‘URI’ and ended up using that.

class ApplicationController \< ActionController::Base  
before\_filter :change\_database

def change\_database  
path = URI.parse(request.env[“REQUEST\_URI”]).path  
comp = path.split(’/’)

```
if comp[1].downcase == 'sheet'
  ActiveRecord::Base.establish_connection(
  :adapter => "XXX",
  :database => "XXX",
  :host => "XXX",
  :username => "XXX",
  :password => "XXX"
  )
elsif comp[1].downcase == 'component'
  ActiveRecord::Base.establish_connection(
  :adapter => "XXX",
  :database => "XXX",
  :host => "XXX",
  :username => "XXX",
  :password => "XXX"
  )
end

```

end  
end

Cheers

Jeff

---

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [January 24, 2006, 12:44pm UTC](https://www.ruby-forum.com/t/parse-request-url/96516/4 "2006-01-24T12:44:27Z")

</div>

Jeff J. wrote:

> Alex Y. wrote:
> 
> > Jeff J. wrote:
> > 
> > > Hello all.
> > > 
> > > Due to the mistakes of the company before I joined 😉 I am stuck with a  
> > > multiple database setup. I am planning on parsing the REQUEST\_URL to  
> > > decide which controller is being accessed and basing the  
> > > establish\_connection on that. Before I go messing around with regexps I

there are easier ways to determine the controller…

params[:controller] in a before\_filter should do the trick…

---

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [January 24, 2006, 12:55pm UTC](https://www.ruby-forum.com/t/parse-request-url/96516/5 "2006-01-24T12:55:41Z")

</div>

Mikkel B. wrote:

> there are easier ways to determine the controller…
> 
> params[:controller] in a before\_filter should do the trick…

heh,

try self.class in a before filter in ApplicationController, its probably  
the best way…
