默认网址在动作邮件中无法正常工作

时间:2014-04-03 15:45:48

标签: ruby-on-rails ruby smtp actionmailer mandrill

我在我的localhost上测试。这是我的development.rb文件的action_mailer部分:

config.action_mailer.delivery_method = :smtp

config.action_mailer.smtp_settings = {
    :address   => "smtp.mandrillapp.com",
    :port      => 587,
    :user_name => "my_email@email.com,
    :password  => ENV['MANDRILL_API_KEY'],

}

config.action_mailer.default_url_options = { :host => 'localhost:3000' }

这是action_mailer文件:

def confirm_email(user, school, email)
   @user = user
   @school = school
   @email = email
   @url  = 'http://example.com/login'
  mail(to: @email, subject: 'Please confirm your additional school email.')
end

以下是观点:

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>Hello <%= @user.first_name %></h1>

    <p>
      To confirm your additional school, <%= link_to "click here", verify_other_school_path(@school) %>.
    </p>
    <p>Thanks and have a great day!</p>
  </body>
</html>

电子邮件正常运行并正常发送。唯一的问题是为链接生成的html如下所示:

<a href="http://verifyotherschool/4" target="_blank">click here</a>

而不是它应该的方式:

<a href="http://localhost:3000/verifyotherschool/4" target="_blank">click here</a>

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

记住path是相对的,而url是绝对的。
而是

  verify_other_school_path(@school)

使用

  verify_other_school_url(@school)

  verify_other_school_path(only_path: false, @school)

并阅读this