Forum: Ruby on Rails Parse request URL.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jeff J. (Guest)
on 2006-01-24 12:12
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
Alex Y. (Guest)
on 2006-01-24 12:18
(Received via mailing list)
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.
Jeff J. (Guest)
on 2006-01-24 12:55
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
Mikkel B. (Guest)
on 2006-01-24 13:44
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...
Mikkel B. (Guest)
on 2006-01-24 13:55
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...
This topic is locked and can not be replied to.