How can a rails app get its host name?

I would like my Rails app to supply a callback url to another app. I currently have a yaml file in the config folder in which I put the host name, but I would like to be able to query that dynamically and do away with the yaml file. Is there a method call that returns the current host?

Sort of - a given request has a host name (look at the methods on ActionController::Request), obviously you can only get at that inside a controller action.

Fred

request.host

explainer wrote:


I would like my Rails app to supply a callback url to another app. I
currently have a yaml file in the config folder in which I put the
host name, but I would like to be able to query that dynamically and
do away with the yaml file. Is there a method call that returns the
current host?
--

You should be able to do "name = system ‘hostname’ " if you are on a linux host. This will not give the name used by a virtual host though.

Try request.env['HTTP_HOST'] in the controller.

That assumes you are on the default port (80).

To find what the machine thinks its own name is (which may or may not be related to the hostname you want in a callback url), try:

  Socket.gethostname

As has been pointed out, the callback url also needs things like the port. If you are generating this within a controller action, you could of course just use the url_for() helper to generate the callback url.

request.env["SERVER_ADDR"] // this will return you server ip address request.server_name or request.host // this will return host name

An example that has caused me to have to use a config.yml, ActionMailer will not have request.* available. System calls would not work in this case either, since they would return a bad value on vhosts or in cases where the hostname is different than request.host.

-eric

request.host_with_port()

in rails 2.3.4 api

I would like my Rails app to supply a callback url to another app. I
currently have a yaml file in the config folder in which I put the
host name, but I would like to be able to query that dynamically and
do away with the yaml file. Is there a method call that returns the
current host?

probably you want to write a little rack middleware app here,

check this link and the comments with code underneath,

http://coderack.org/users/laktek/entries/11-server-proxy

check database.yml file in config directory