加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 20:37:35 所属栏目:百科 来源:网络整理
导读:我试图在包含全路径(即 – http://localhost/contacts/id/confirm)的邮件邮件中放置一个Rails link_to语句.我正在尝试的link_to语句在我的标准View in / pages / options中工作,但不在Mailer电子邮件中. 这是我的/ pages / options控制器代码: class PagesC
我试图在包含全路径(即 – http://localhost/contacts/id/confirm)的邮件邮件中放置一个Rails link_to语句.我正在尝试的link_to语句在我的标准View in / pages / options中工作,但不在Mailer电子邮件中.

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

class PagesController < ApplicationController
    def options
    end
end

这里是页面/选项查看:

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

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

<!DOCTYPE html>
<html>
<head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
</head>
<body>
    <%= link_to "here",:id => 17 %>
</body>
</html>

错误信息:

RuntimeError in Contacts#create

Showing C:/Documents and Settings/Corey Quillen/My Documents/Dev/Dev    
Projects/my_project
Project/my_project/app/views/user_mailer/welcome_email.html.erb where line #7  
raised:

Missing host to link to! Please provide :host parameter or set  
default_url_options[:host]
Extracted source (around line #7):

4:     <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
5:   </head>
6:   <body>
7:     <%= link_to "here",:only_path    
=> false,:id => 17 %>
8:   </body>
9: </html>

解决方法

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

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读