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

发布于 2024-09-30 01:44:05 字数 1669 浏览 8 评论 0原文

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

<p><%= link_to 'here', unsubscribe_path(:email => "[email protected]") %></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 => "[email protected]") %></p> to unsubscribe</p>
</body>
</html>

这是邮件程序:

class Alert < ActionMailer::Base

def send(alert, email)
  @subject = "hi"
  @recipients = "[email protected]"
  @from = "[email protected]"
  @sent_on = Time.now
  @content_type = "text/html"

  @alert = alert
  @email = email
end

结束

I strongly feel its a silly error and I'm somehow not able to see through it. I'm trying to use this piece of code in the view of my mailer.

<p><%= link_to 'here', unsubscribe_path(:email => "[email protected]") %></p>

And I've defined a named route in my routes file :

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

So when I send my email this is the link I get:

http://b.comonly_pathtruecontrollerusersactionunsubscribe/

Any ideas ? Thanks !

EDIT

Here's the view:

<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 => "[email protected]") %></p> to unsubscribe</p>
</body>
</html>

And here's the mailer:

class Alert < ActionMailer::Base

def send(alert, email)
  @subject = "hi"
  @recipients = "[email protected]"
  @from = "[email protected]"
  @sent_on = Time.now
  @content_type = "text/html"

  @alert = alert
  @email = email
end

end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筱果果 2024-10-07 01:44:05

这是一个工作模板的示例,我不能确定为什么你的模板不起作用,但请尝试在通知程序中生成 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         "[email protected]"
    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 [email protected].", :line_width => 60

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

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

编辑:
这应该有效:(

#notifier
body         :unsubscribe_url => unsubscribe_url(:email => "[email protected]")

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

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

Here's an example of a working template, I can't say for sure why yours isn't working but try generating the url in the notifier.

# 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         "[email protected]"
    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 [email protected].", :line_width => 60

You will need to generate a url, rather than a path.

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

edit:
this should work:

#notifier
body         :unsubscribe_url => unsubscribe_url(:email => "[email protected]")

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

(see above example for how this all fits together)

不必在意 2024-10-07 01:44:05

这个 unsubscribe_url (或 _path)在任何其他视图中都有效吗?

返回的值看起来像是由哈希转换为字符串组成的。尝试对“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"=>"[email protected]"}
  # ..and so on
end

Does this unsubscribe_url (or _path) work in any other view?

The returned value looks like a something made from a Hash converted to String. Try to call .inspect on the object returned by "link_to()" - maybe there will be some clue?

Maybe something redefined a link_to method somewhere in your code? Use "grep def.*link_to" - maybe some clues will be there?

A test may be useful in this and later cases. Here is an example (however this is what I use in rails 1, but I hope this does not differ too much in your rails version).

def test_routing
  assert_routing '/unsubscribe', {:controller => 'user', :action => 'unsubscribe'}
  assert_recognizes Hash[:controller => 'user', :action => 'unsubscribe'],
      {:path=>'/unsubscribe', :method => :get},
      {"email"=>"[email protected]"}
  # ..and so on
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文