rosnk
rosnk

Reputation: 1098

truncate text in homepage which has been posted using ckeditor in ruby on rails framework

I am using ckeditor with combination with carrierwave in textarea (https://github.com/galetahub/ckeditor). I am using ruby on rails as a framework.

I am trying to truncate article to display only a small section in homepage which has been published from backend using ckeditor. However since i have also uploaded image for the article using same ckeditor truncate is not working. And .html_safe is also not working showing me the html code instead of rendering it.

Code used in homepage to display only a small section using truncate function:

     <% i=0  %>

              <% @diplomacies.each do |diplomacy_article| %>

                <% if i ==0 %>
                    <h3><a><%= diplomacy_article.title %></a></h3>  
                    <p><%= truncate(diplomacy_article.article.html_safe, :length=>600) %> </p>
                <% end %>
                <% i=i+1 %>

              <% end %>

Current Result in home page:

<p><img alt="" src="/uploads/ckeditor/pictures/2/content_pm.jpg" style="float:left; height:81px; margin-left:4px; margin-right:4px; width:100px" />&nbsp;Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error, nobis tempore, ad temporibus iure enim recusandae. Laborum ratione, accusamus quis amet recusandae tempora vitae? Reiciendis ab similique doloremque exercitationem saepe. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptas vero voluptatem voluptatibus accusamus, doloremque aspernatur earum saepe repellat cupiditate architecto quidem hic ut tempora quod, dolore i... 

Current problem line:

<%= truncate(diplomacy_article.article.html_safe, :length=>600) %>

Current Controller index method:

def index
category_diplomacy= Category.where(["name=?", "Diplomacy"]).first
 if category_diplomacy
 @diplomacies=Article.where(["category_id=?","#{category_diplomacy.id}" ]).order("id DESC").limit(7).all
 end

end

####### Response to the answer provided by: @Ruby Racer

Truncation does not seem to work. I have tried following code:

Thanks for the reply, however it did not worked for me. 

I tried the following code, however truncate does not seem to work.

 <% i=0  %>

              <% @diplomacies.each do |diplomacy_article| %>
              <% orig_text = diplomacy_article.article %>
              <% img_text  = orig_text.match(/<img.+(\/)?>/).to_s if orig_text.match(/<img.+(\/)?>/) %>
              <% body_text = truncate(orig_text.gsub(/<.*>/,''), :length=>30) %>

                <% if i ==0 %>
                    <h3><a><%= diplomacy_article.title %></a></h3>
                    <p><%= "#{img_text} #{body_text} ".html_safe  %> </p>
                <% end %>

                <% i=i+1 %>

              <% end %>

I have tried to output all of your suggested code.

1)Code: <%= orig_text = diplomacy_article.article %>

Output:

 Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error, nobis tempore, ad temporibus iure enim recusandae. Laborum ratione, accusamus quis amet recusandae tempora vitae? Reiciendis ab similique doloremque exercitationem saepe. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptas vero voluptatem voluptatibus accusamus, doloremque aspernatur earum saepe repellat cupiditate architecto quidem hic ut tempora quod, dolore incidunt rem maiores corporis!

2) <%= img_text = orig_text.match(//).to_s if orig_text.match(//) %>

Output:  Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error, nobis tempore, ad temporibus iure enim recusandae. Laborum ratione, accusamus quis amet recusandae tempora vitae? Reiciendis ab similique doloremque exercitationem saepe. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptas vero voluptatem voluptatibus accusamus, doloremque aspernatur earum saepe repellat cupiditate architecto quidem hic ut tempora quod, dolore incidunt rem maiores corporis!

3) <%= body_text = truncate(orig_text.gsub(/<.*>/,''), :length=>10) %>

Output: {no output}

Upvotes: 0

Views: 789

Answers (2)

Payal Jain
Payal Jain

Reputation: 1

Or you can also use like that:

<%
   orig_text = diplomacy_article.article
   body_text = truncate(orig_text,:length=>600, :omission => "" , :escape => false)
%>

And to output (sample html):

<%= body_text.html_safe %>

Upvotes: 0

Ruby Racer
Ruby Racer

Reputation: 5740

If I understand correctly, you want the image and you want the body text.
Regular Expressions can do most of the work here:

EDIT
I have corrected the regex to create the image tag if it exists. So img_text will either contain an image tag or a blank string.
body_text is meant to contain everything except the html tags that once were there (with or without the <br>'s). This has also been edited.

So, what will basically be there is:

<%
    orig_text = diplomacy_article.article
    img_text  = orig_text.match(/<img[^>]+>/).to_s 
    body_text = truncate(orig_text.gsub(/<[^>]+>/,''), :length=>600) # without br's
#or body_text = truncate(orig_text.gsub(/<[^([Bb][Rr])^>]+>/,''), :length=>600) # with br's
%>

And to output (sample html):

<div class="my_image">
    <%= img_text.html_safe %>
</div>
<div class="my_abstract">
    <%= body_text.html_safe %>
</div>

This should be working and img_text will output an image only if an image tag exists in the body.

Upvotes: 1

Related Questions