# Getting a complete URL from Rails

**URL:** <https://www.ruby-forum.com/t/getting-a-complete-url-from-rails/92679>\
**Category:** Rails\
**Created:** [April 23, 2007, 12:38am UTC](https://www.ruby-forum.com/t/getting-a-complete-url-from-rails/92679 "2007-04-23T00:38:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![miles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/47e85d/32.png) [@miles](https://www.ruby-forum.com/u/miles)\
**Post date:** [April 23, 2007, 12:38am UTC](https://www.ruby-forum.com/t/getting-a-complete-url-from-rails/92679/1 "2007-04-23T00:38:03Z")

</div>

Hello,

I’m sending out an e-mail message to new users of my service. I want  
to send a particular link in this e-mail but I can’t figure out how to  
get a complete URL from the url\_for method. Is there a way to do this  
without manually setting the host name?

Thank you,  
– Miles

---

<div class="post-metadata">

**Author:** ![miles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/47e85d/32.png) [@miles](https://www.ruby-forum.com/u/miles)\
**Post date:** [April 23, 2007, 12:50am UTC](https://www.ruby-forum.com/t/getting-a-complete-url-from-rails/92679/2 "2007-04-23T00:50:20Z")

</div>

Miles wrote:

> Hello,
> 
> I’m sending out an e-mail message to new users of my service. I want  
> to send a particular link in this e-mail but I can’t figure out how to  
> get a complete URL from the url\_for method. Is there a way to do this  
> without manually setting the host name?
> 
> Thank you,  
> – Miles

Have you tried to pass :only\_path =\> false ?

Greeting  
Skade

---

<div class="post-metadata">

**Author:** ![miles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/47e85d/32.png) [@miles](https://www.ruby-forum.com/u/miles)\
**Post date:** [April 23, 2007, 12:59am UTC](https://www.ruby-forum.com/t/getting-a-complete-url-from-rails/92679/3 "2007-04-23T00:59:04Z")

</div>

I did, what I have in my mail template is this…

> \<%= url\_for :controller =\> “friend”, :action=\>“confirm\_email”,:only\_path=\> false %\>

The error I get is this…

> Missing host to link to! Please provide :host parameter or set default\_url\_options[:host]

I can pass in the host name, but then when I deploy my production  
environment I’ll have to remember to go in and change it. I’m looking  
around now to see if there’s a way to set it just once (maybe in the  
environment file).

If there’s a way Rails can just figure it out, though, that would be  
ideal. Are there global variables I can access, maybe, that I could  
use to build out the beginning of the URL (the host name and the  
application’s path)?

– Miles

On Apr 22, 6:50 pm, Florian G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![miles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/47e85d/32.png) [@miles](https://www.ruby-forum.com/u/miles)\
**Post date:** [April 23, 2007, 3:05pm UTC](https://www.ruby-forum.com/t/getting-a-complete-url-from-rails/92679/4 "2007-04-23T15:05:01Z")

</div>

On Apr 22, 2007, at 10:15 PM, jemminger wrote:

> > > > to send a particular link in this e-mail but I can’t figure out  
> > > > Greeting
> > > 
> > > – Miles
> 
> wouldn’t request.host work as the host parameter?  
> [Peak Obsession](http://api.rubyonrails.com/classes/ActionController/)  
> AbstractRequest.html#M000251

If the problem is that your mailer isn’t running as a “request”, then  
why don’t you set a variable in your config/environments/  
development.rb (and perhaps also in your production.rb) to get the  
host where you need it.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![miles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/47e85d/32.png) [@miles](https://www.ruby-forum.com/u/miles)\
**Post date:** [April 23, 2007, 4:16am UTC](https://www.ruby-forum.com/t/getting-a-complete-url-from-rails/92679/5 "2007-04-23T04:16:17Z")

</div>

wouldn’t request.host work as the host parameter?  
[http://api.rubyonrails.com/classes/ActionController/AbstractRequest.html#M000251](http://api.rubyonrails.com/classes/ActionController/AbstractRequest.html#M000251)

---

<div class="post-metadata">

**Author:** ![miles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/47e85d/32.png) [@miles](https://www.ruby-forum.com/u/miles)\
**Post date:** [April 24, 2007, 2:10pm UTC](https://www.ruby-forum.com/t/getting-a-complete-url-from-rails/92679/6 "2007-04-24T14:10:34Z")

</div>

Yes, the code that sens the mail message in an Mailer object, it’s  
outside the request/response cycle so it’s request object is always  
nil. I was thinking about putting this information in the environment  
somewhere, but then I’d have to change it at deployment time and that  
would be kind of a drag.

Passing the request object to the Mailer from the calling Controller  
is a good idea! 🙂 I hadn’t thought of that, that will solve this  
problem, I think.

Thank you,  
– Miles

---

<div class="post-metadata">

**Author:** ![miles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/47e85d/32.png) [@miles](https://www.ruby-forum.com/u/miles)\
**Post date:** [April 25, 2007, 7:15am UTC](https://www.ruby-forum.com/t/getting-a-complete-url-from-rails/92679/7 "2007-04-25T07:15:36Z")

</div>

Maybe this…

Write URLs from arbitrary places in your codebase, such as your  
mailers.

Example:

class MyMailer  
include ActionController::UrlWriter  
default\_url\_options[:host] = ‘[www.basecamphq.com](http://www.basecamphq.com)’

```
def signup_url(token)
  url_for(:controller => 'signup', action => 'index', :token =>

```

token)  
end  
end

---

<div class="post-metadata">

**Author:** ![miles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/47e85d/32.png) [@miles](https://www.ruby-forum.com/u/miles)\
**Post date:** [April 24, 2007, 10:55am UTC](https://www.ruby-forum.com/t/getting-a-complete-url-from-rails/92679/8 "2007-04-24T10:55:01Z")

</div>

Hi,

> > Missing host to link to! Please provide :host parameter or set default\_url\_options[:host]
> 
> I can pass in the host name, but then when I deploy my production  
> environment I’ll have to remember to go in and change it. I’m looking  
> around now to see if there’s a way to set it just once (maybe in the  
> environment file).

I had the same problem as you. In the mailer you cannot get the host,  
because mailer is something belonging to a model, so it’s out of the  
request/response cycle because of design (that’s the way to go with  
MVC).

You are right that you need to pass the host parameter to your mailer,  
but in some cases you won’t need to put it on the configuration file. If  
the mailer gets called from a controller (as a result of one action of  
the user, for example) then you can just use  
“#{request.protocol}#{request.host\_with\_port}” and you’ll get the url  
for the root of your application.

Unfortunately, if you are going to invoke the mailer from a cron or a  
task, then there’s no request and you should stick to the configuration  
constant solution.

Regards,

javier ramirez

## –

Estamos de estreno… si necesitas llevar el control de tus gastos  
visita [http://www.gastosgem.com](http://www.gastosgem.com) !!Es gratis!!
