Fotios
Fotios

Reputation: 3663

Determine Rails server URI

I feel like I'm just missing an environmental variable here, but is there anyway to get the FQDN and port that a Rails server is listening on? I feel like there should be a variable like RAILS_URL or something. I'd like to access it from a model.

Upvotes: 0

Views: 1518

Answers (2)

scrrr
scrrr

Reputation: 5340

I think you are looking for root_url.

Upvotes: 2

Terry G Lorber
Terry G Lorber

Reputation: 2962

I've gotten around this by setting an environment variable with the application's URL as part of the script that runs Mongrel. The env. var. is then available to Ruby, and you can even set a global like RAILS_ROOT in /config/environment.rb

You can also investigate the request object: http://perma-link.appspot.com/k

request.domain
request.port

Pass values returned from the request object into the Model when needed.

class Bar < AR::Base 
  def self.active_for_domain(domain)
    find(:all, :conditions => ["deleted <> true and domain = ?", domain])
  end
end

class FooController < ApplicationController
  def index
    @bars = Bar.active_for_domain(request.domain)
  end
end

Upvotes: 0

Related Questions