【问题标题】:How to include full-path in Rails 3 link_to statement?如何在 Rails 3 link_to 语句中包含完整路径?
【发布时间】:2012-02-22 06:05:53
【问题描述】:

我正在尝试将 Rails link_to 语句放在包含完整路径的 Mailer 电子邮件中(即 - http://localhost/contacts/id/confirm)。我正在尝试的 link_to 语句在 /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", :controller => "contacts", :action => "confirm",
     :only_path => false, :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", :controller => "contacts", :action => "confirm", :only_path    
=> false, :id => 17 %>
8:   </body>
9: </html>

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3


    【解决方案1】:

    第一步:

    #config/environments/production.rb
    config.action_mailer.default_url_options = { :host => 'www.example.com' }
    
    #config/environments/development.rb
    config.action_mailer.default_url_options = { :host => 'localhost:3000' }
    

    第二步:

    <%= link_to "here", confirm_contacts_url(17) %>
    

    【讨论】:

    • 谢谢zolter。其他人,如果这仍然不起作用,要确保的一件重要但微妙的事情是您使用的是 confirm_contacts_url() 而不是 confirm_contacts_path()。 _path 总是返回一个相对 url,即使主机在你的配置中设置正确。
    【解决方案2】:

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

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

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

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

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

    【讨论】:

    • 完美运行!感谢您(和其他人)的帮助!
    【解决方案3】:

    根据当前的指南http://guides.rubyonrails.org/action_mailer_basics.html#generating-urls-in-action-mailer-views,我认为最好的方法是使用 url_for 并在环境配置文件中配置主机。或者更好的是使用名称路由。

    命名路由示例

    link_to @user.fullname, user_url(@user)
    

    【讨论】:

      【解决方案4】:

      您需要提供:host 选项和link_to

      您还可以将 config/environments/*.rb 文件中的 config.action_mailer.default_url_options 设置为适当的设置,以便在所有邮件中为 link_to 选择它们

      例如-

      在 config/environments/production.rb 中

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

      【讨论】:

        【解决方案5】:

        我认为在以电子邮件格式使用主机时,您需要在过滤器之前传递主机。我记得当我遇到类似问题时使用此页面:http://www.cherpec.com/2009/06/missing-host-to-link-to-please-provide-host-parameter-or-set-default_url_optionshost/

        这里也有一个 SO 帖子,但有不同的看法

        【讨论】:

          【解决方案6】:

          如果完整的 url 始终与用户的请求相匹配,您也可以使用 actionmailer-with-request gem 将请求转发到操作邮件程序,然后您可以在邮件模板中引用该请求,例如:

          <%= link_to "log into the admin page", request.base_url + admin_root_path %> 
          

          【讨论】:

            猜你喜欢
            • 2015-08-16
            • 1970-01-01
            • 2014-05-09
            • 2011-07-25
            • 1970-01-01
            • 2016-09-10
            • 1970-01-01
            • 2011-01-26
            • 2011-01-08
            相关资源
            最近更新 更多