如何在haml中渲染tinymce编辑器内容?

时间:2018-11-26 12:06:00

标签: ruby-on-rails tinymce haml

我开始学习Rails上的ruby并创建博客。我已经为博客内容集成了tinyMCE编辑器。但是我不知道如何为前端渲染tinyMCE内容。现在,它以html形式显示内容。 enter image description here

内容呈现代码:

%div.container
   %h1.text-center Blogs List
   = link_to 'New Blog', '/admin/blogs/new', :class => 'btn btn-info'            
   = link_to 'logout', '/admin/logout' , :class => 'btn btn-info'
   %table
      %tr
         %th.text-center Title
         %th.text-center Category
         %th.text-center Content
         %th.text-center Feature Image
         %th.text-center{:colspan => 3} Operations
      -@blogs.each do |blog|
         %tr
            %td
               %h4= blog.title
               %td= render blog.categories
            %td.mb-4= blog.text  #editor content
            %td= link_to 'Download', blog.featuredImage_url ,:class=> 'thumbnail'
            %td= link_to 'Show', admin_blog_path(blog) , :class=>'btn btn-info'
            %td= link_to 'Edit', edit_admin_blog_path(blog), :class=> 'btn btn-info'
            %td= link_to 'Destroy', admin_blog_path(blog), :class=> 'btn btn-info', |
               method: :delete,
               data: { confirm: 'Are you sure?' }

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用.sanitize这样的方法:

%td.mb-4= sanitize(blog.text)
  

清理HTML输入,删除所有未列入白名单的标签和属性。

您可以使用(例如)配置列入白名单的标签列表:

# In config/application.rb
config.action_view.sanitized_allowed_tags = ['strong', 'em', 'a']
config.action_view.sanitized_allowed_attributes = ['href', 'title']

.sanitize method doc