在邮件程序中使用 REST-ful link_to 时出现问题
我强烈地感觉到这是一个愚蠢的错误,而且我无法看清它。我正在尝试在我的邮件程序的视图中使用这段代码。
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个工作模板的示例,我不能确定为什么你的模板不起作用,但请尝试在通知程序中生成 url。
您将需要生成一个 url,而不是路径。
编辑:
这应该有效:(
请参阅上面的示例,了解这一切如何组合在一起)
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.
You will need to generate a url, rather than a path.
edit:
this should work:
(see above example for how this all fits together)
这个 unsubscribe_url (或 _path)在任何其他视图中都有效吗?
返回的值看起来像是由哈希转换为字符串组成的。尝试对“link_to()”返回的对象调用 .inspect - 也许会有一些线索?
也许在代码中的某个地方重新定义了 link_to 方法?使用“grep def.*link_to” - 也许会有一些线索?
在这种情况和以后的情况下,测试可能会很有用。这是一个示例(不过这是我在 Rails 1 中使用的,但我希望这在您的 Rails 版本中不会有太大差异)。
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).