【问题标题】:Rails 3 and devise escaping mailer templatesRails 3 并设计转义邮件模板
【发布时间】:2011-06-15 04:14:38
【问题描述】:
我正在使用 rails 3 并设计用于身份验证。
将我的邮件模板转换为除设计默认值之外的任何内容都会导致 html 转义。这样做的一个特殊后果是新帐户电子邮件的确认包含无效的确认令牌。
<p><a href=3D"http://localhost:3000/users/confirmation?confirmation_token=
=3D88uo7jetcetc">Confirma=
r mi cuenta</a></p>
前面的 3d 是 html 转义,不应该存在。使用 raw 和 html_safe 对输出没有影响。
【问题讨论】:
标签:
ruby-on-rails
ruby-on-rails-3
escaping
devise
actionmailer
【解决方案1】:
请注意,此效果有时仅存在于控制台中,而在用于生产和/或发送的实际电子邮件中时不存在。我就是这种情况;它与非英文字符无关,但在我修改确认电子邮件后确实创建了上面提到的“3D”和“=3D”插入 - 但仅在控制台输出中 - 使测试复杂化。
在 Devise Github 上查看此问题:
http://github.com/plataformatec/devise/issues/2086
...它被归结为“Rails 问题”。
如果有人知道一张公开的票,我希望看到这个补丁,因为它可以为许多第一次遇到它的其他开发人员节省很多时间,并简化所有人的测试。
【解决方案2】:
好的,如果有人遇到这个问题,这就是我解决它的方法。
如果邮件模板包含任何非英文字符,则整个模板将被转义。除了在 rails 之前转义所有此类字符之外,似乎没有其他方法可以解决此问题。
例如:
%p
= "!Bienvenido, #{username}!"
%p
Usted puede confirmar su cuenta a través del siguiente enlace:
%p
= link_to 'Confirmar mi cuenta', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token.html_safe)
需要
%p
= "¡Bienvenido, #{username}!"
%p
Usted puede confirmar su cuenta a través del siguiente enlace:
%p
= link_to 'Confirmar mi cuenta', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token.html_safe)
【解决方案3】:
我也有这个问题。 html_safe 在我使用 rails link_to 方法后起作用。
错误代码
%a{:href => "#{@redemption.link.html_safe}"}= "http://example.com/#{@redemption.model.email_hash}"
好代码
= link_to "http://example.com/#{@redemption.model.email_hash}", @redemption.link.html_safe
【解决方案4】:
我可以确认在控制台中收到了这样的电子邮件(带有前面的 3D),但不是在真实发送的消息中。所以,没有必要逃避任何事情。
但是,出于测试目的,您可以添加以下代码以仅在开发模式下在控制台中显示电子邮件:
<% if Rails.env.development? %>
<% logger.debug confirmation_url(@resource, confirmation_token: @token) %>
<% end %>