Forum: Ruby on Rails Getting a complete URL from Rails

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C812af97480d1ed752603936d73f7744?d=identicon&s=25 Miles (Guest)
on 2007-04-23 00:38
(Received via mailing list)
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
D1f1c20467562fc1d8c8aa0d328def62?d=identicon&s=25 Florian Gilcher (skade)
on 2007-04-23 00:50
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
C812af97480d1ed752603936d73f7744?d=identicon&s=25 Miles (Guest)
on 2007-04-23 00:59
(Received via mailing list)
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 Gilcher <rails-mailing-l...@andreas-s.net>
D69d23d8e811e8ab2a8593380d6ede63?d=identicon&s=25 Jeff Emminger (jemminger)
on 2007-04-23 04:16
(Received via mailing list)
wouldn't request.host work as the host parameter?
http://api.rubyonrails.com/classes/ActionControlle...
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-04-23 15:05
(Received via mailing list)
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?
> 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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
1f2eadfb41362800ebc2cf211b91d0f7?d=identicon&s=25 javier ramirez (Guest)
on 2007-04-24 10:55
(Received via mailing list)
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 !!Es gratis!!
C812af97480d1ed752603936d73f7744?d=identicon&s=25 Miles (Guest)
on 2007-04-24 14:10
(Received via mailing list)
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
21f7ed21f11a809050594c82eab11d67?d=identicon&s=25 Robert Walker (Guest)
on 2007-04-25 07:15
(Received via mailing list)
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'

    def signup_url(token)
      url_for(:controller => 'signup', action => 'index', :token =>
token)
    end
 end
This topic is locked and can not be replied to.