如何在 Rails 3 link_to 语句中包含完整路径? [英] How to include full-path in Rails 3 link_to statement?

查看:32
本文介绍了如何在 Rails 3 link_to 语句中包含完整路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Rails link_to 语句放入包含完整路径的 Mailer 电子邮件中(即 - http://localhost/contacts/id/确认).我正在尝试的 link_to 语句适用于/pages/options 中的标准视图,但不适用于 Mailer 电子邮件.

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

class PagesController <应用控制器定义选项结尾结尾

这里是页面/选项视图:

<%= link_to "here", :controller =>"联系人", :action =>确认",:only_path =>假,:id =>17%>

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

<头><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/><身体><%= link_to "here", :controller =>"联系人", :action =>确认",:only_path =>假,:id =>17%></html>

错误信息:

Contacts#create 中的运行时错误显示 C:/Documents and Settings/Corey Quillen/My Documents/Dev/Dev项目/my_projectProject/my_project/app/views/user_mailer/welcome_email.html.erb where line #7提高:缺少要链接的主机!请提供 :host 参数或设置default_url_options[:host]提取的源代码(围绕第 7 行):4:<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>5:</头>6:<身体>7: <%= link_to "here", :controller =>"联系人", :action =>确认",:only_path=>假,:id =>17%>8:</body>9:</html>

解决方案

因为邮件程序不在响应堆栈内运行,所以它们不知道从哪个主机调用它们:这就是您遇到此错误的原因.很容易修复,更改代码以包含主机:

<%= link_to "here", :controller =>"联系人", :action =>确认",:only_path =>假,:id =>17, :host =>example.com"%>

您还可以通过指定以下内容,在 application.rb(或任何环境)内基于每个应用程序设置默认主机:

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

有关 ActionMailer 的完整文档以及出现此问题的原因,请查看 ActionMailer 文档.

I am trying to put a Rails link_to statement inside a Mailer email that includes the full-path (ie - http://localhost/contacts/id/confirm). The link_to statement that I am trying works in my standard View in /pages/options, but not in the Mailer email.

Here is my /pages/options Controller code:

class PagesController < ApplicationController
    def options
    end
end

And here's the pages/options View:

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

When I put this link into the following mailer (welcome_email.html.rb), I am getting the error below. Any help with this will be greatly appreciated.

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

The error message:

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", :controller => "contacts", :action => "confirm", :only_path    
=> false, :id => 17 %>
8:   </body>
9: </html>

解决方案

Because mailers aren't run inside the response stack, they have no idea what host they were called from: that's why you're running into this error. It's easy to fix, change the code to include the host:

<%= link_to "here", :controller => "contacts", :action => "confirm",
:only_path => false, :id => 17, :host => "example.com" %>

You can also set the default host on a per-application basis inside of your application.rb (or any of your environments) by specifying this:

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

For the full documentation on ActionMailer and why this problem occurs, check out the ActionMailer documentation.

这篇关于如何在 Rails 3 link_to 语句中包含完整路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆