2017-02-10 5 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 

私の挑戦は私がURLで正しくハイパーリンクclicking hereテキストを取得することができないということであるI必要。それはそれをレンダリングしません。私は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 

とあなたのuser_mailer.rb内部html_safeディレクティブで明示的なHTMLを送信してください:

def dynamic_actual_digest(email, html_text) 
    mail(to: email) do |format| 
    format.html { render html: html_text.html_safe } 
    end 
end 
関連する問題