Rails:模型中未定义的方法“截断" [英] Rails: Undefined method `truncate' in model

查看:34
本文介绍了Rails:模型中未定义的方法“截断"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型中有以下方法可以裁剪记录的描述,但由于未知原因,截断方法不起作用:

I have the following method in my model which crops the record's description, but for an unknown reason the truncate method does not work:

def cropped_description
  nb_words_max = 500
  if description.length > nb_words_max
    truncate(description, :length => nb_words_max, :separator => ' ') + " ..."
  else
    description
  end
end

有人看到我做错了吗?谢谢.

Anyone sees what I'm doing wrong? Thanks.

推荐答案

你用错了,你应该在 String 上调用这个方法.查看 truncate 的签名.

You're using it wrong, you should be calling this method on a String. See truncate's signature.

使用:

if description.length > nb_words_max
  description.truncate(nb_words_max, :separator => ' ') + " ..."
else
  ...

这篇关于Rails:模型中未定义的方法“截断"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆