How can a rails app get its host name?

8,755 views
Skip to first unread message

explainer

unread,
Nov 19, 2009, 2:55:50 PM11/19/09
to Ruby on Rails: Talk
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?

Frederick Cheung

unread,
Nov 19, 2009, 2:57:49 PM11/19/09
to Ruby on Rails: Talk
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

icke

unread,
Nov 19, 2009, 6:44:03 PM11/19/09
to Ruby on Rails: Talk
request.host

Norm Scherer

unread,
Nov 19, 2009, 6:47:31 PM11/19/09
to rubyonra...@googlegroups.com
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.

Scott Johnson

unread,
Nov 20, 2009, 2:03:38 PM11/20/09
to Ruby on Rails: Talk
That assumes you are on the default port (80).

candlerb

unread,
Nov 20, 2009, 4:05:08 PM11/20/09
to Ruby on Rails: Talk
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.

sami

unread,
Nov 21, 2009, 11:00:58 AM11/21/09
to Ruby on Rails: Talk

request.env["SERVER_ADDR"] // this will return you server ip address
request.server_name or request.host // this will return host name
Syed Samiuzzaman
software Engineer
Code71 Inc (www.code71.com)
product: www.scrumpad.com
blog: samiuzzaman.blogspot.com

Eric

unread,
Nov 21, 2009, 1:13:25 PM11/21/09
to Ruby on Rails: Talk
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

On Nov 19, 11:57 am, Frederick Cheung <frederick.che...@gmail.com>
wrote:

shweta

unread,
Nov 20, 2009, 1:06:03 AM11/20/09
to Ruby on Rails: Talk
Try request.env['HTTP_HOST'] in the controller.

tommy xiao

unread,
Nov 24, 2009, 11:30:26 PM11/24/09
to rubyonra...@googlegroups.com
request.host_with_port()

in rails 2.3.4 api

2009/11/20 shweta <shweta....@gmail.com>
Try request.env['HTTP_HOST'] in the controller.

--

You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=.





--
tommy xiao
E-mail: xiaods(AT)gmail.com

Andres Paglayan

unread,
Nov 25, 2009, 1:01:08 AM11/25/09
to rubyonra...@googlegroups.com
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

INDRANIL MUKHERJEE

unread,
Nov 25, 2009, 3:14:43 AM11/25/09
to Ruby on Rails: Talk
check database.yml file in config directory
Reply all
Reply to author
Forward
0 new messages