我正在写一个博客,作为一种形式,我wysiwyg
使用froala editor
. 我还缩短了索引页面上的帖子内容,我使用truncate
它的方法。问题是该.html_safe
方法(应该将编辑表单中的内容显示html
为纯文本而不是代码)不能与truncate
方法串联工作。所以,这里是代码:
index.html.erb
<% @posts.each do |post| %>
<h3 class="title">
<%= post.title %>
<small class="date">
| <%= post.created_at.strftime('%B, %d, %Y') %>
</small>
</h3>
<p class="fr-view">
<%= truncate(post.content, length: 100).html_safe %>...
<%= link_to ' Read more', post %>
</p>
<% end %>
_form.html.erb
<%= form_for @post, role:"form" do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<div class="form-group">
<%= f.label :content %><br>
<%= f.text_area :content, id:"edit", rows:"3", class:"form-control" %>
</div>
<p>
<%= f.submit 'Create', class:'btn btn-primary' %>
</p>
<% end %>
<script>
$(function() {
$('#edit').froalaEditor()
});
</script>
这就是post.content
in 控制台返回的内容:
Post.find(1)
<Post id: 1, title: "Lorem Ipsum", content: "<p>Lorem ipsum dolor sit amet, consectetur adipisc..."