views:

483

answers:

2

hi, I am sending an email that contains a link to my website. I want to be able to test it locally and be able to move the scripts around to different hosts easily.

In my email right now I use the following:

  <%= url_for(:host => 'localhost:3000', :controller => "user_activations", :action => "show", :id=>@id, :confirm=>@passcode) %>

This works for when testing locally but will obviously fail for production. Is there an easy way to have rails (or ruby) detect what the current host is? I'm thinking something like $_SERVER of php.

I realize I can use some logic using my environment variable but I would like to avoid this.

Thanks

A: 

I define a constant 'HOST' in my environment.rb that sets my host. Alternatively you can use request.host or request.domain.

jonnii
Put HOST = "localhost:3000" your environments/development.rb and the live one in environments/production.rb ie HOST = "www.myhost.com"
Lee Irving
+1  A: 

in environments/development.rb

config.action_mailer.default_url_options = { :host => "localhost", :port => 3000 }

in environments/production.rb

config.action_mailer.default_url_options = { :host => "www.xyu.at" }

and use tests with rspec-email :)

Lichtamberg
just to note, an easy way to do this is to use a yaml config file and then just user default_url_options = yml[ENV]
Ori Cohen