[英]Rails replacing '\n' to '<br/>' issue
輸入是文本區域類型
所以我像這樣從控制器獲取數據,
'firstline \n secondline \n thirdline"
我想將其更改為
'firstline <br/> secondline <br/> thirdline"
我用谷歌搜索,發現了這個。
description = appointment[:description].to_s.gsub(/\n/, "<br/>")
但是很奇怪的是,如果我打印出這個字樣,它會顯示出這樣一個奇怪的文字,
碼,
<%=Rails.logger.debug("***description***" + description)%>
結果,
<br/>3rd linen***1st line
更奇怪的是,我必須將這些參數放在這樣的javascript函數中,
<span class="hotspot" onmouseover="tooltip.show(<%=user_list%>,"<%=description%>");" onmouseout="tooltip.hide();">
<%=h appointment[:users] %>
</span>
專注於<%= description%>
在這里,我遇到了一個錯誤,並顯示了這樣的內容,
<span class="hotspot" onmouseover="tooltip.show([],"1st line
<br/>2nd line
<br/>3rd line");" onmouseout="tooltip.hide();">
Uncaught SyntaxError: Unexpected token ILLEGAL
有什么好主意嗎?
嘗試這個
"firstline \n secondline \n thirdline".to_s.gsub(/\n/, "<br/>").html_safe
也許這會工作
desc = appointment[:description].to_s
description = desc.split("\n").join("<br/>")
嘗試這個。
description = appointment[:description].to_s.gsub(/\n/, "<br/>").html_safe
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.