2016-06-08 81 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