Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails-显示文本字段中的字符数_Ruby On Rails_Ruby_Text - Fatal编程技术网

Ruby on rails rails-显示文本字段中的字符数

Ruby on rails rails-显示文本字段中的字符数,ruby-on-rails,ruby,text,Ruby On Rails,Ruby,Text,我正在运行一个rails项目,并且我正在显示一个文本字段,该字段通常可能太长。如果我可以调用任何东西在查看页面上仅显示20个单词或120个字符???您可能对TextHelper的功能感兴趣: truncate("Once upon a time in a world far far away") # => Once upon a time in a world f... truncate("Once upon a time in a world far far away",

我正在运行一个rails项目,并且我正在显示一个文本字段,该字段通常可能太长。如果我可以调用任何东西在查看页面上仅显示20个单词或120个字符???

您可能对TextHelper的功能感兴趣:

  truncate("Once upon a time in a world far far away")
  # => Once upon a time in a world f...

  truncate("Once upon a time in a world far far away", :length => 14)
  # => Once upon a...

  truncate("And they found that many people were sleeping better.", :length => 25, "(clipped)")
  # => And they found that many (clipped)

  truncate("And they found that many people were sleeping better.", :omission => "... (continued)", :length => 15)
  # => And they found... (continued)
你也可以看到这个例子


在ruby中,您还可以为字符串指定一个范围。mystring[0..119]
'Once upon a time in a world far far away'.truncate(27)
"Once upon a time in a wo..."

'Once upon a time in a world far far away'.truncate(27, separator: ' ')
"Once upon a time in a..."

'Once upon a time in a world far far away'.truncate(27, separator: /\s/)
"Once upon a time in a..."

'And they found that many people were sleeping better.'.truncate(25, omission: '... (continued)')
"And they f... (continued)"