1

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

<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

结尾

4

2 回答 2

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

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

于 2010-10-28T08:15:07.433 回答
1

这个 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
于 2010-10-28T09:39:01.347 回答