2014-10-17 139 views
-1

我正在使用表单发布到博客。在博客展示帖子页面上,我使用rails内容显示标签上的==来解释html。如何隐藏HTML标签?

但是,我有一个博客索引页面,显示最新的帖子,我想截断的帖子预览不解释html,但同时隐藏html标签。

我该怎么做?

def blog 
    #@content3 = Content.find(3) 
    @blogPosts = Blogpost.all.reverse.drop(1) 
    @latestPost = Blogpost.last 
    if @latestPost.blank? == false 
     @blogPicture = Blogpost.last.image 
    end 
    respond_to do |format| 
     format.html { render :layout => 'blog' } 
    end 
    end 
+0

请让我们看看你的代码。 – Severin 2014-10-17 20:15:13

+0

<%= truncate(@ latestPost.content,:length => 200,:separator =>'')%> – Callum 2014-10-17 20:18:56

+0

添加控制器代码到主线程: – Callum 2014-10-17 20:19:16

回答

0

您需要使用full_sanitizer,然后是truncate的结果。

例(在控制器中使用):

sanitized = ActionView::Base.full_sanitizer.sanitize(@string) 
@truncated = ActionView::Helpers::TextHelper.truncate(sanitized, length: 17)