2016-06-08 80 views
1

當我使用html_safe,爲什麼我在rails 4.2.6的ruby中使用simple_format時會丟失格式?

<%= @micropost.content.html_safe %> 

我得到正確的格式下輸出。

enter image description here

但是當我使用simple_format,我失去​​的中心對齊,並獲得行名單,我不想打破。

<%= simple_format(auto_link(@micropost.content, html: { target: '_blank' }), {}, :sanitize => false) %> 

enter image description here

這是我所得到的,當我使用auto_link單獨與消毒假。

<%= auto_link(@micropost.content, html: { target: '_blank' }, sanitize: false) %> 

enter image description here

這是我所得到的,當我獨自使用auto_link的sanitize與真實。

<%= auto_link(@micropost.content, html: { target: '_blank' }, sanitize: true) %> 

enter image description here

這是我在後

>> micp.content 
=> "<p style=\"text-align: center;\">Pictre </p><h2 style=\"text-align: center;\">restse</h2><h2>sfsdfsdf</h2><p>sdfdsf</p><p style=\"text-align: center;\">dsfds</p><p>sfsdfsdf</p><ol><li>sdfdsfsdf</li><li>sdfsdfdsf</li><li>sdfdsf</li></ol><div>dfsdfsdfsd</div><p style=\"text-align: center;\">dfsdf</p><ol><li>dsfsdf</li><li>sdf</li><li>sdfsd</li></ol><p style=\"text-align: center;\"><br></p>" 
>> 

我如何解決這一點,有哪個用戶在他/她的職位想要的格式?謝謝。

回答

0

這是一個從API文檔:

simple_format - 返回文本使用簡單 格式規則轉化成HTML。兩個或更多個連續的換行符(\ n \ n)是 被視爲一個段落幷包裹在<p>標記中。一個換行符(\ n)是 被視爲換行符,並且附加了<br />標籤。此方法 不會從文本中刪除換行符。

所以不是:

<%= simple_format(auto_link(@micropost.content, html: { target: '_blank' }), {}, :sanitize => false) %> 

你或許應該做到以下幾點:

<%= auto_link(@micropost.content, html: { target: '_blank' }, sanitize: false) %> 

參考simple_formatrails_autolink獲取更多信息。

+0

我試過你的解決方案,我只拿到html標籤。我已經更新了問題的輸出。 – LovingRails

+0

嘗試用'sanitize:true' – Dharam

+0

我現在得到輸出,但仍然失去格式。 – LovingRails