新手问题 Rails 里面如何正确截断过长的文本?

LinuxGit · August 24, 2014 · Last by Rei replied at August 25, 2014 · 2038 hits

http://www.railstutorial.org/book/user_microposts 练习第 6 题:Very long words currently break our layout, as shown in Figure 10.18. Fix this problem using the wrap helper defined in Listing 10.47. Note the use of the raw method to prevent Rails from escaping the resulting HTML, together with the sanitize method needed to prevent cross-site scripting. This code also uses the strange-looking but useful ternary operator。

按教程增加了一个 MicropostsHelper,定义了 wrap(content) 方法,发现不起作用,需要在哪里调用吗还是自动使用 helper 中方法过滤。 我的代码提交在https://github.com/LinuxGit/weibo/blob/master/app/helpers/microposts_helper.rb 求教,谢谢,这块困了不少时间。

我的现在是这样的。

@huacnlee @Rei 😢 看书的方法是每 30 个字符间加个​ 我的不起作用。我的这个问题太简单了吗,为啥被移动到 NoPoint 了。

标题有问题吗?我加个不生效。

helper 需要调用:

<!-- app/views/shared/_micropost_item.html.erb -->
<span class="content"><%= wrap object.content %></span>

其实这里可以用 css:

// css
.content {
  word-wrap: break-word;
}

#4 楼 @Rei 谢谢。 我的标题的确不够精确,只是描述问题,下次会注意。

#5 楼 @LinuxGit 我觉得原本帖子也没什么问题,不是我移动的。

You need to Sign in before reply, if you don't have an account, please Sign up first.