`
yangzb
  • 浏览: 3471249 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

undefined method `length' for Enumerable when calling truncate method

    博客分类:
  • Ruby
阅读更多

With rails 2.0.2 and ruby 1.8.7 you can find this error using rails truncate helper

To solve this problem, paste this code in your enviroment.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

Have a nice day!

Incompatible with Ruby 1.8.7

If using Rails < 2.2 with Ruby 1.8.7, calling truncate will result in the following error:

  undefined

 method

 `length

'

 for #<Enumerable::Enumerator:0xb74f952c>




The workaround (other than upgrading to Rails 2.2 or higher), is to overwrite the truncate method, by inserting the following at the end of environment.rb (or where it will be called on startup):

分享到:
评论

相关推荐

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: The TFlexPanel.FindControlAtPoint method maked virtual to realize RealTime-capability when on mouse cursor moving the flex-object search not occurs. - FIX: After deleting the selected points ...

    前端开源库-node-enumerable

    前端开源库-node-enumerable节点可枚举,ES6就绪LINQ功能以typescript编写

    Recursively Enumerable Sets and Degrees(Soare)

    Recursively Enumerable Sets and Degrees by Rovert I. Soare。递归论的书籍,Soare的,从djvu转过来的,有可能有误。经典的教材

    通过Enumerable实现线程控制(源码 更新)

    根据AsyncEnumerator的原理改写,提供了线程函数的流程控制,线程管理,监视函数调用超时的功能。 适用于需要开启多个线程,要去线程能够长时间稳定执行某一操作的场景 不足之处欢迎留言

    Prototype使用指南之enumerable.js

    Enumerable是一个抽象对象(需要说明的是,javascript中并没有类的概念,所指的类也就是一个函数,继承一般指的是一个对象(父)将它的方法属性copy(通过Object.extend, copy的是引用)到子类(函数)的prototype属性(一...

    Enumerable.js:适用于所有类型集合的实用函数

    这个库很大程度上受到 Ruby 的 Enumerable 模块的启发,并借用了它的大量功能。 注意:这并不意味着是一个独立的库,而是要与具有.each方法的另一种数据类型结合使用。安装作为 NPM 模块npm install enumerable-js ...

    CSharp 4.0 .Net Framework V4.0 Enumerable 类

    CSharp 4.0 .Net Framework V4.0 Enumerable 类

    enumerable.lua:一个lua集合库

    local Enumerable = require ( ' enumerable ' ) collectionInstance = Enumerable. create ({ 1 , 2 , 3 , 4 , 5 }) secondElement = collectionInstance[ 2 ] collectionInstance: each ( function ( value , ...

    enumerable-methods

    该项目展示了Ruby Enumerable模块中方法的重建列表。 重写的Enumerable方法是: 每个-&gt; my_each each_with_index-&gt;​​ my_each_with_index 选择-&gt; my_select 全部? -&gt; my_all? 任何? -&gt; my_any? 没有...

    C#使用LINQ中Enumerable类方法的延迟与立即执行的控制

    主要介绍了C#的LINQ查询中Enumerable类方法的延迟与立即执行,LINQ语言集成查询可以让C#和VB以查询数据库相同的方式操作内存数据,需要的朋友可以参考下

    mongoid_enumerable:Mongoid 可枚举

    gem 'mongoid_enumerable' 然后执行: $ bundle 或者自己安装: $ gem install mongoid_enumerable 用法 只需在您的文档中包含MongoidEnumerable 。 添加enumerable后: 字段名称 具有可能值的数组 选项( ...

    enumerable-statistics

    Enumerable::Statistics 提供了一些方法来计算数组和枚举中的统计汇总。 安装 将此行添加到应用程序的 Gemfile 中: gem 'enumerable-statistics' 然后执行: $ bundle 或者自己安装: $ gem install ...

    enumerable4j:Ruby的惊人“ Enumerable”移植到Java

    概述enumerable4j是Ruby著名的移植到java 。如何使用在获取最新版本: &lt; dependency&gt; &lt; groupId&gt;io.dgroup&lt;/ groupId&gt; &lt; artifactId&gt;enumerable4j&lt;/ artifactId&gt; ${version}&lt;/ version&gt;&lt;/ dependency&gt; 所需的Java...

    Prototype源码浅析 Enumerable部分之each方法

    在javascript中,根本找不到Enumerable的影子,因为这一块是Prototype作者从Ruby中借鉴过来的。并且Enumerable在实际中根本没有直接应用的机会,都是混入到其他的对象中,可以说是其他对象的一个“父类”(不过只是...

    enumerable-java-0.4.0.jar

    jar包,官方版本,自测可用

    【JavaScript源代码】JavaScript 中的六种循环方法.docx

    JavaScript 中的六种循环方法  Javascript中的遍历循环  对于数值索引的数组来说,可以使用标准的for循环来遍历值 ... Object.defineProperty(myobject,"a",{ //可枚举 enumerable:true, value:2, }) Object

    Prototype使用指南之array.js

     将iterable转化为数组,如果iterable定义了toArray方法,就调用这个方法,否则利用iterable的length属性进行枚举, 如果iterable没有length属性的话就返回空数组[] Array对象除了扩展Enumerable对象的方法外,...

    enumerable-java-weaver-0.4.0.jar

    jar包,官方版本,自测可用

    enumerable-java-0.4.0-sources.jar

    jar包,官方版本,自测可用

    LINQPad_Premium_5.36.03_Any_CPU 含破解

    When dumping a non-enumerable item to a grid, nullable bools no longer cause a formatting error. When displaying XML documentation summaries in autocompletion listings and tooltips, &lt;para&gt; tags are no...

Global site tag (gtag.js) - Google Analytics