2017-02-10 152 views
0

我正在尝试构建一个字符串,该字符串将作为电子邮件的正文传递给UserMailer。无法在电子邮件上正确显示链接

下面是代码:

html_text = "" 
topic_object.title = "Example title" 
topic_object.body = "Example body" 
html_text << 'Visit the page by <a href="http://localhost.com/topic_digests/#{topic_object.slug}>" clicking here</a>.<br>' 
html_text << topic_object.title 
html_text << topic_object.body 

那么我这一行提供的电子邮件

UserMailer.dynamic_actual_digest(current_user.email, html_text).deliver 

我的挑战是,我不能让clicking here文本与正确的URL我的超链接需要。它不呈现它。我试过link_to,我试过双引号,我试过<%= topic_object.slug %>

我相信问题在于,即使使用link_to方法或a html标记,也需要同一行上的双引号和单引号。

我缺少什么?

回答

2

使用%Qsyntax构建字符串:

html_text = "" 
topic_object.title = "Example title" 
topic_object.body = "Example body" 
html_text << %Q|Visit the page by <a href="http://localhost.com/topic_digests/#{topic_object.slug}>" clicking here</a>.<br>| 
html_text << topic_object.title 
html_text << topic_object.body 
UserMailer.dynamic_actual_digest(current_user.email, html_text).deliver 

,并尝试与html_safe指令发送明确的HTML您user_mailer.rb内:

def dynamic_actual_digest(email, html_text) 
    mail(to: email) do |format| 
    format.html { render html: html_text.html_safe } 
    end 
end 
相关问题