# Getting host server name

**URL:** <https://www.ruby-forum.com/t/getting-host-server-name/81618>\
**Category:** Rails\
**Created:** [January 10, 2007, 5:53pm UTC](https://www.ruby-forum.com/t/getting-host-server-name/81618 "2007-01-10T17:53:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [January 10, 2007, 5:53pm UTC](https://www.ruby-forum.com/t/getting-host-server-name/81618/1 "2007-01-10T17:53:05Z")

</div>

Is there a way to pull the host server information in Ruby on Rails?  
Such as a command?

I have links linking back to my RoR web application, but my web  
application can and will move back and forth between servers and instead  
of constantly changing these links to accommodate the server, is there a  
way to define or pull server information and insert it in front of the  
link as a variable? Thanks.

Gilles

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [January 10, 2007, 7:26pm UTC](https://www.ruby-forum.com/t/getting-host-server-name/81618/2 "2007-01-10T19:26:09Z")

</div>

> Is there a way to pull the host server information in Ruby on Rails?  
> Such as a command?
> 
> I have links linking back to my RoR web application, but my web  
> application can and will move back and forth between servers and instead  
> of constantly changing these links to accommodate the server, is there a  
> way to define or pull server information and insert it in front of the  
> link as a variable? Thanks.

You could inspect the ‘request’ object and see what’s there…, but if  
it’s  
just for links, you could look at the :only\_path =\> false part of  
url\_for.

But, why do you need absolute links?

-philip

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [January 10, 2007, 7:45pm UTC](https://www.ruby-forum.com/t/getting-host-server-name/81618/3 "2007-01-10T19:45:03Z")

</div>

Philip H. wrote:

> You could inspect the ‘request’ object and see what’s there…, but if  
> it’s  
> just for links, you could look at the :only\_path =\> false part of  
> url\_for.
> 
> But, why do you need absolute links?
> 
> -philip

Can you elaborate on that?

Im using absolute links because I’m using action mailer. So I’m sending  
an email out when an entry in a table is created. The links are in these  
emails. So say I create an entry. I, and other people, then receive an  
email with a link back to this entry. Right now the link is based on my  
localhost, but if the web application were moved, it would refer to the  
wrong host. Does that make sense?

Gilles

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [January 10, 2007, 7:53pm UTC](https://www.ruby-forum.com/t/getting-host-server-name/81618/4 "2007-01-10T19:53:10Z")

</div>

> Can you elaborate on that?
> 
> Im using absolute links because I’m using action mailer. So I’m sending  
> an email out when an entry in a table is created. The links are in these  
> emails. So say I create an entry. I, and other people, then receive an  
> email with a link back to this entry. Right now the link is based on my  
> localhost, but if the web application were moved, it would refer to the  
> wrong host. Does that make sense?

Yep! That’s a good reason to have full urls 🙂

I’d look at the request object as well :only\_path =\> false

url\_for(options = {}, \*parameters\_for\_method\_reference)

Returns a URL that has been rewritten according to the options hash and  
the defined Routes. (For doing a complete redirect, use redirect\_to). A  
url\_for is used to: A All keys given to url\_for are forwarded to the  
Route module, save for the following:

```
 * :anchor specifies the anchor name to be appended to the path. 

```

For  
example, url\_for :controller =\> posts, :action =\> show, :id =\> 10,  
:anchor  
=\> comments will produce “/posts/show/10#comments”.  
\* :only\_path if true, returns the absolute URL (omitting the  
protocol, host name, and port)  
\* :trailing\_slash if true, adds a trailing slash, as in  
“/archive/2005/”. Note that this is currently not recommended since it  
breaks caching.  
\* :host overrides the default (current) host if provided  
\* :protocol overrides the default (current) protocol if provided
