2017-02-10 98 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 
相關問題