ruby-on-rails – 如何在Rails 3中包含全路径link_to语句?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 如何在Rails 3中包含全路径link_to语句?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在包含全路径(即 – http://localhost/contacts/id/confirm)的邮件邮件中放置一个Rails link_to语句.我正在尝试的link_to语句在我的标准View in / pages / options中工作,但不在Mailer电子邮件中.

这是我的/ pages / options控制器代码

  1. class PagesController < ApplicationController
  2. def options
  3. end
  4. end

这里是页面/选项查看:

  1. <div>
  2. <%= link_to "here",:controller => "contacts",:action => "confirm",:only_path => false,:id => 17 %>
  3. </div>

当我将此链接放入以下邮件程序(welcome_email.html.rb)时,我收到以下错误.对此的任何帮助将不胜感激.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <Meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
  5. </head>
  6. <body>
  7. <%= link_to "here",:id => 17 %>
  8. </body>
  9. </html>

错误信息:

  1. RuntimeError in Contacts#create
  2.  
  3. Showing C:/Documents and Settings/Corey Quillen/My Documents/Dev/Dev
  4. Projects/my_project
  5. Project/my_project/app/views/user_mailer/welcome_email.html.erb where line #7
  6. raised:
  7.  
  8. Missing host to link to! Please provide :host parameter or set
  9. default_url_options[:host]
  10. Extracted source (around line #7):
  11.  
  12. 4: <Meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
  13. 5: </head>
  14. 6: <body>
  15. 7: <%= link_to "here",:only_path
  16. => false,:id => 17 %>
  17. 8: </body>
  18. 9: </html>

解决方法

因为邮件程序没有在响应堆栈中运行,他们不知道他们被调用的主机:这就是为什么你遇到这个错误.很容易修复,更改代码以包含主机:
  1. <%= link_to "here",:id => 17,:host => "example.com" %>

您还可以通过指定以下方式在application.rb(或任何环境)中的每个应用程序的基础上设置默认主机:

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

有关ActionMailer的完整文档以及为什么会出现此问题,请查看ActionMailer documentation.

猜你在找的Ruby相关文章