开发者

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 s开发者_C百科erver 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.


I think you are looking for root_url.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜