【问题标题】:Problem with using the REST-ful link_to while using it in a mailer在邮件程序中使用 REST-ful link_to 时出现问题
【发布时间】:2010-10-28 07:17:29
【问题描述】:

我强烈地感觉到这是一个愚蠢的错误,而且我不知何故无法看穿它。我正在尝试在我的邮件程序视图中使用这段代码。

<p><%= link_to 'here', unsubscribe_path(:email => "a@b.com") %></p>

我在路由文件中定义了一个命名路由:

map.unsubscribe '/unsubscribe', :controller => :users, :action => :unsubscribe

所以当我发送电子邮件时,这是我得到的链接:

http://b.comonly_pathtruecontrollerusersactionunsubscribe/

有什么想法吗?谢谢 !

编辑

这是视图:

<html>
<body>
<p>
Hi, <br />

You have an alert for : <%= @alert.name %>. <br />

Here is some brief information about it: <br />

<%= @alert.details %><br /><br />
<br />Thank You,<br />
</p>
<p>
Click <p><%= link_to 'here', unsubscribe_path(:email => "a@b.com") %></p> to unsubscribe</p>
</body>
</html>

这是邮件:

class Alert < ActionMailer::Base

def send(alert, email)
  @subject = "hi"
  @recipients = "xyz@mail.com"
  @from = "xyz@mail.com"
  @sent_on = Time.now
  @content_type = "text/html"

  @alert = alert
  @email = email
end

结束

【问题讨论】:

  • 为什么不直接使用link_to 'here', 'http://xxx.com/unsubscribe?email=a@b.com'
  • 可以的。但是这样硬编码网址不是一个好习惯,是吗?无论如何,谢谢,必须使用它,直到我得到一个解决方案来保持它的 REST-ful。
  • 请在您的问题中包括您是如何生成电子邮件的。路由没问题,所以它必须是你的通知器或模板。
  • 当然马克。我现在已经添加了。
  • 可能想从此处的代码中编辑您的真实电子邮件地址...

标签: ruby-on-rails restful-url


【解决方案1】:

这是一个工作模板的示例,我不能确定为什么您的模板不起作用,但请尝试在通知程序中生成 url。

# notifier
def new_message_email(response)
    I18n.locale = response.recipient.language
    subject      I18n.t(:you_have_received_new_message_from_sender, :sender => response.sender.login)
    from         "info@domain.com"
    recipients   response.recipient.email
    content_type "text/html"
    sent_on      Time.now
    body         :sender_login => response.sender.login, :recipient_login => response.recipient.login
  end

#template
= word_wrap "Hi #{@recipient_login}."
= word_wrap ""
%p
= word_wrap "You have received a new personal message from #{@sender_login}.", :line_width => 60
= word_wrap "Click #{link_to 'here', account_inbox_url} to view your domain.com message inbox.", :line_width => 60
= word_wrap "If the above URL does not work try copying and pasting it into your browser. If you continue to have problems, please feel free to contact us at support@domain.com.", :line_width => 60

您需要生成一个 url,而不是路径。

eg:
account_path => /account
account_url => http://domain.com/account

编辑: 这应该工作:

#notifier
body         :unsubscribe_url => unsubscribe_url(:email => "a@b.com")

#template
= word_wrap "Click #{@unsubscribe_url} to unsubscribe.", :line_width => 60

(请参阅上面的示例了解这一切如何组合在一起)

【讨论】:

    【解决方案2】:

    此 unsubscribe_url(或 _path)在任何其他视图中是否有效?

    返回的值看起来像是一个由 Hash 转换为 String 的东西。尝试对“link_to()”返回的对象调用 .inspect - 也许会有一些线索?

    也许某些东西在您的代码中某处重新定义了 link_to 方法?使用“grep def.*link_to”——也许会有一些线索?

    在这种情况和以后的情况下,测试可能会很有用。这是一个示例(但这是我在 rails 1 中使用的,但我希望这在您的 rails 版本中不会有太大差异)。

    def test_routing
      assert_routing '/unsubscribe', {:controller => 'user', :action => 'unsubscribe'}
      assert_recognizes Hash[:controller => 'user', :action => 'unsubscribe'],
          {:path=>'/unsubscribe', :method => :get},
          {"email"=>"a@example.com"}
      # ..and so on
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-11
      • 2012-07-25
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 2011-03-28
      • 2010-11-20
      相关资源
      最近更新 更多