0

So I am stuck with a small issue while migrating a legacy project from Rails 3.2 to 4.2 Inside one of our views (erb), the code (Rails 3.2)

html_safe(truncate(job_profile.description + '&nbsp;&nbsp;<font color="#999">[ ' + list.join(', ') +' ]</font>', :length => 100))

produces the following text:

Rails_3_version

if it's not clear job_profile.description resolves to Test and likewise list.join(', ') to English Listening Comprehension Now the same code in Rails 4.2 produces the following:

Rails_4_version

Tried many options to reproduce the original result but didn't succeed. Suggestions ?

Akash Srivastava
  • 151
  • 1
  • 10

2 Answers2

0

Do this instead

truncate(job_profile.description + '&nbsp;&nbsp;<font color="#999">[ ' + list.join(', ') +' ]</font>', :length => 100).html_safe

or

raw(truncate(job_profile.description + '&nbsp;&nbsp;<font color="#999">[ ' + list.join(', ') +' ]</font>', :length => 100))

Hope that helps!

Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78
0

Ok I guess I found a solution, using a truncate over html_safe that includes special characters was producing problems, when the entire text including the nbsp; and inline styling was cut short due to truncate.

Switching the order of html_safe and truncate somewhat solved the issue. Now it would truncate first and then apply html_safe. But still was producing undesirable results when total length including all special characters increased my truncate limit. A little effort produced a result which was almost correct.

html_safe(job_profile.description.truncate(100) + '&nbsp;&nbsp;<font color="#999">[ ' + truncate(list.join(', '), length: 100-(job_profile.description.length)) +' ]</font>')

Little more customisation, taking into accounts the three dots ... every truncate produces, and one would get the exact result as needed.

Akash Srivastava
  • 151
  • 1
  • 10