I'm trying to use the Rails truncate helper inside of Sidekiq. Everything works fine outside of Sidekiq but inside sidekiq when I try to process the job it gives me:
NoMethodError: undefined method `each_byte' for nil:NilClass
This is because Sidekiq contains it's own truncate method:
http://www.rubydoc.info/gems/sidekiq/3.3.1/Sidekiq/WebHelpers:truncate
The problem is it's too simple compared to the Rails helper and I need to be able to truncate words as opposed to chars. So I tried to use the method explictly:
content = ActionView::Helpers::TextHelper.truncate(content.gsub(/\n/, ''), :length => 126, :separator => ' ', :omission => '', :escape => false)
But this still gave me the same error as before.
Additionally, I also tried including the helper in the top of the class
include ActionView::Helpers::TextHelper
But that didn't seem to make any difference. Any ideas how I can use the Rails truncate method inside Sidekiq?
Thanks,
Just open source code by your link above Source: hide | on GitHub
Source: hide | on GitHub
:
def truncate(text, options = {}, &block)
if text
length = options.fetch(:length, 30)
content = text.truncate(length, options) // <--
content = options[:escape] == false ? content.html_safe : ERB::Util.html_escape(content)
content << capture(&block) if block_given? && text.length > length
content
end
end
As you can see this method just using truncate
method of String . Just use it instead of ActionView helper.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.