Wednesday, March 3, 2010

truncate method incompatible with Ruby 1.8.7

With rails > 2.2 and ruby 1.8.7 we can find this error using rails truncate helper

undefined method `length' for Enumerable::Enumerator:0x7f44da230548>


To solve this problem paste the below code in environment.rb (EOF)



module ActionView
module Helpers
module TextHelper
def truncate(text, length = 30, truncate_string = "...")
if text.nil? then return end
l = length - truncate_string.chars.to_a.size
(text.chars.to_a.size > length ? text.chars.to_a[0...l].join + truncate_string : text).to_s
end
end
end
end




No comments: