Estou desenvolvendo um programa no trabalho e a função truncate funciona perfeitamente. Porém, quando fiz o upload para o BlueHost me deparei com o seguinte erro:
(undefined method 'length' for #<Enumerable::Enumerator:0xXXXXXXXXXXX>)
Isso foi algo que estragou minha tarde de ontem…
Procurando no google, descobri que o problema é a versão do rails.
No meu PC de desenvolvimento, na raiz do meu aplicativo rails, digito o comando script/about para descobrir a versão do ruby/rails, e obtenho o seguinte resultado:
About your application's environment
Ruby version 1.8.6 (i486-linux)
RubyGems version 0.9.4
Rails version 2.0.2
Active Record version 2.0.2
Action Pack version 2.0.2
Active Resource version 2.0.2
Action Mailer version 2.0.2
Active Support version 2.0.2
Edge Rails revision unknown
Application root /home/user/projetos/meuprojeto
Environment development
Database adapter mysql
Database schema version 8
Já no meu servidor:
About your application's environment
Ruby version 1.8.7 (x86_64-linux)
RubyGems version 1.1.1
Rails version 2.0.2
Active Record version 2.0.2
Action Pack version 2.0.2
Active Resource version 2.0.2
Action Mailer version 2.0.2
Active Support version 2.0.2
Application root /home/user/projetos/meuprojeto
Environment development
Database adapter mysql
Database schema version 8
Só uma pequena mudança e um bom estrago…
Poderia mudar a função no text_helper.rb do rails, mas acho que não ficaria legal, então, simplesmente abri o application_helper.rb e criei a função trucar com o mesmo código fonte do rails 1.8.6.
def truncar (text, length = 30, truncate_string = "...")
if text.nil? then return end
l = length - truncate_string.length
if $KCODE == "NONE"
text.length > length ? text[0...l] + truncate_string : text
else
chars = text.split(//)
chars.length > length ? chars[0...l].join + truncate_string : text
end
end
Para usar é a mesma coisa que o truncate original. Ex:
truncar(produto.descricao, 40)
Claro, cheira a gambiarra, mas sou meio newbie mesmo, e até conseguir uma solução mais elegante vou tocando com essa mesmo.
