限制视图中的字符/单词 - ruby​​ on rails [英] Limiting characters/words in view - ruby on rails

查看:43
本文介绍了限制视图中的字符/单词 - ruby​​ on rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Ruby on Rails 构建的一个非常简单的博客应用程序的主页上显示最近的评论.我想限制从评论表的正文"列显示的字符数.我假设我可以在 <%=h comment.body %> 的代码末尾添加一些东西,但我不知道那会是什么,因为我是 Ruby 和 Rails 的新手.

I am displaying recent comments on the home page of a very simple blog application I am building in Ruby on Rails. I want to limit the number of characters that are displayed from the 'body' column of the comments table. I am assuming I can just add something to the end of the code for <%=h comment.body %> but I don't know what that would be yet as I am new to both Ruby and Rails.

这是我在/views/posts/index.html.erb 文件中的代码:

Here is the code I have in the /views/posts/index.html.erb file:

<% Comment.find(:all, :order => 'created_at DESC', :limit => 5).each do |comment| -%>
    <p>
        <%=h comment.name %> commented on 
        <%= link_to h(comment.post.title), comment.post %><br/>
        <%=h comment.body %>
        <i> <%= time_ago_in_words(comment.created_at) %> ago</i>
    </p>
    <% end -%>

推荐答案

试试 截断 视图助手

<%=h truncate(comment.body, :length => 80) %>

这篇关于限制视图中的字符/单词 - ruby​​ on rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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