Question

I am using https://github.com/ejholmes/active_admin_editor to create my blogposts.

So on the blog index page I need to display the first 100 characters of the body in html_safe so its not raw html code, but this also makes the link active. For example, if in my first few lines I have an outbound link, I need to make the link_to blogpost_path(bp) override any links I have in the html_safe body content.

#post-wrapper
    - @blogposts.each do |bp|
      = link_to blogpost_path(bp) do
        .blogpost
          .blog-title
            %h2= bp.title
            .blog-image
              - unless bp.attachment.url.include?('missing')
                = image_tag bp.attachment.url(:medium)
          .blog-author
            %p= "by #{bp.author}"
          .blog-date
            %p= time_tag bp.created_at
          .blog-body
            %p= bp.body[0..100].html_safe
Was it helpful?

Solution

I think that the right thing to do is to strip away all the HTML tags that you don't want in these 100 chars.

You could use strip_tags to strip everything or maybe strip_links to remove only the links.

Here you can find the details: http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html#method-i-strip_tags

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top