Search code examples
ruby-on-railsrubytinymcehaml

how to render tinymce editor content in haml?


I'm start learning ruby on rails and create blogs. I have integrate tinyMCE editor for blogs content. but i don`t know how to render tinyMCE content for front-end. Now it shows content with as it is html. enter image description here

Content Rendering Code:

%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?' }

Thankyou!!


Solution

  • You could use .sanitize method like :

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

    Sanitizes HTML input, stripping all tags and attributes that aren’t whitelisted.

    You can configure list of whitelisted tags with (for example) :

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

    .sanitize method doc