2016-08-08 18 views
4

我的模型有聯繫人:字符串和電子郵件:字符串屬性。在純HTML,我們可以寫什麼是mailto等效的導軌方式:和tel:?

<a href="tel:123-456">123-456</a> 

我們如何將這些兩成Rails代碼假設我的型號名稱爲樣本。我的猜測會像

<%= link_to @sample.email, "#" %> 

<%= link_to @sample.contact, "#" %> 

什麼應該在"#"

回答

9

使用mail_to "[email protected]"而不是link_to

對於tel:link_to @sample.contact, "tel:#{number}"

+0

'<(%)=的link_to @ sample.contact, 「電話:#{@ sample.contact}」 %>' –

1

使用mail_to

<%= mail_to @sample.email, "Email us" %> 

對於電話號碼,您可能必須編寫自己的幫助程序,或者將「tel:」串聯到url中。

0

簡單tel_to幫手tel:個鏈接:

def tel_to(phone_number) 
    phone_number = number_to_phone(phone_number) 
    link_to phone_number, "tel:#{phone_number}" 
end 

tel_to(8424176704) 
# => <a href="tel:842-417-6704">842-417-6704</a> 

而對於mailto現有mail_to

mail_to("Dorian", "[email protected]") 
# => "<a href=\"mailto:Dorian\">[email protected]</a>"