在邮件程序中使用REST-ful link_to时出现问题

时间:2010-10-28 07:17:29

标签: ruby-on-rails restful-url

我强烈地感到这是一个愚蠢的错误,我不知道怎么也看不到它。我试图在邮件的视图中使用这段代码。

<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

2 个答案:

答案 0 :(得分:1)

以下是一个工作模板的示例,我不能确定为什么你的工作模板无效但尝试在通知程序中生成网址。

# 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

您需要生成网址,而不是路径。

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

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

答案 1 :(得分:1)

这个unsubscribe_url(或_path)是否可以在任何其他视图中使用?

返回的值看起来像是由转换为String的Hash创建的。尝试在“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