Parse request URL


#1

Hello all.

Due to the mistakes of the company before I joined :wink: 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


#2

Jeff J. wrote:

Hello all.

Due to the mistakes of the company before I joined :wink: 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.


#3

Alex Y. wrote:

Jeff J. wrote:

Hello all.

Due to the mistakes of the company before I joined :wink: 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


#4

Jeff J. wrote:

Alex Y. wrote:

Jeff J. wrote:

Hello all.

Due to the mistakes of the company before I joined :wink: 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…


#5

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…