1

If i am starting rails server in different system with same environment, i need to get dynamic host. how can i get the host and send it to body of the mail.

Right now am using below configuration,

in config/development.rb: 

config.action_mailer.default_url_options = { :host => "example.com" }

in app/mailers/notifications.html.erb :

<%= test_url(id) %>
Galet
  • 5,853
  • 21
  • 82
  • 148
  • So what is the problem with the configuration ? – sjain May 19 '14 at 07:13
  • @VedPrakash if two rails server are running in two system with same(dev) environment. How can i differentiate the host of two system. – Galet May 19 '14 at 07:19

2 Answers2

1

You can pass the request when calling the mailer function from the controller--

class UserMailer < ActionMailer::Base

  def welcome_email(user, request)
    UserMailer.default_url_options[:host] = request.host_with_port #option1
    @user = user
    @url  = user_url(@user, host: request.host_with_port ) #option2 (do this for each link)
    mail(:to => user.email,
         :subject => "Welcome to My Awesome Site")
  end
end

In the above code request.host_with_port is the "example.com" for your case.

So above is the more dynamic way to provide the request host as you can see that you can pass the request when calling the mailer function from the controller.

This is the source that you can check - Generating-urls-in-action-mailer-views.

This is also the explantion at action-mailer-default-url-options-and-request-host which is marked here for the answer to read.

Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185
  • I am calling Mailer model using background process instead of call using controller. Then how can i get different host. – Galet May 19 '14 at 10:15
0

This uniform treatment of different application servers is mostly a feature.

If you want to differentiate I would suggest setting and using environment variables.

For example you export an APP_SERVER variable with different values in the host machines and then you access them from your Ruby/Rails code as

ENV['APP_SERVER']
Kostas Rousis
  • 5,918
  • 1
  • 33
  • 38