Rails blog 首页文章怎么部分显示……

zhbinx · 2012年11月05日 · 最后由 lonely21475 回复于 2014年11月12日 · 4920 次阅读

同志们,无知的我又来了 我用了<%= raw(blog.content) %>显示了全文 但是我想做到 wordpress 那种只显示部分 然后点查看全文才显示全部的那种效果 应该怎么做呢

truncate 不过对中文有些不太友善 具体原因可以 google 不过一般够用了

<%= truncate(raw(blog.content),:length=>800) 这样的话出来的都是 html 代码了

<p> &nbsp;</p> <p style="margin: 15px 0px; padding: 0px; line-height: 1.4em; color: rgb(0, 0, 0); font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 14px; "> One of the most commonly overlooked and under-refined elements of a website is its pagination controls. In many ca... 

<%= raw(truncate(blog.content),:length=>800)

不行不行,遇到 content 里标签正好没结束的,跟下面页面标签搞上了,直接把页面弄乱了

在 3 楼搞错了,直接把 raw 移出来了,后面的对应括号也得移出去。<%= raw(truncate(blog.content,:length=>800)) %> 如果要求结束标签也对应,在字内容分析上得费点功夫,不知道有没有相应的插件。一般博客首页的摘要都是去掉 html 修饰的吧 <%= truncate(blog.content,:length=>800).gsub(/<.*?>/,'') %>

还有一个办法,把截取后的内容放在 table 里面,只要博客内容不含 table 标签,浏览器在解析的时候会自动修复没有结束的标签

写 post 的时候,单独写一个 summary,显示 summary 就可以了。 summary 如果没有写,在截取字符串。 直接截取有可能出现半截标签,要截取 p 标签,完整的一个 p 标签,不要用字数来截取,应该可以较好的解决。

先 strip_tags,再截字: http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html#method-i-strip_tags

不过这个方法性能不高,如果有大量的 html 需要做类似处理,推荐用 hpricot 来 strip

#7 楼 @woaigithub 👍 我先暂时套个 div,然后限制 div 长度……凑合一下,把东西基本做完之后再试试

<%= (truncate(strip_tags(post.body), :length => 60, : omission => '...')).html_safe %> 

我会弄一个冗余字段存下纯文本

在写文章时在合适的地方加上一个 <!--more-->的标签,然后首页这样显示:

<%= raw post.content.split('<!--more-->')[0] %>

有个 gem 可以按 HTML 标签截取文本,不会把 HTML“切断”,参见这里 http://rubygems.org/gems/truncate_html 我自己的博客就是这么干的,在保存前冗余一个字段 https://github.com/edokeh/klog/blob/master/app/models/blog.rb

#13 楼 @edokeh 我用truncate 怎么都把中文给过滤掉了?

#14 楼 @pobing 不会吧,我那边好好的哎,你看下我的代码和 gem 的版本号

#15 楼 @edokeh 我用的最新的 gem, 也使用了 markdown gem 'truncate_html', '0.9.2 ' <%=raw truncate_html(markdown(post.content, length: 200, omission: '...(continued)') )%> 中文内容,截取后只剩下了标点符号!

17 楼 已删除

#16 楼 @pobing 不知道是你贴错了还是本来代码就写错了,markdown 方法后面为啥少个括号?

markdown(post.content)

#18 楼 @edokeh 呵呵,应该是贴错了,之前我使用 markdown 渲染的,但现在感觉有些文中内容太长了,就想部门显示,所以就看到了你推荐的 truncate_html,另外 omission: '...(continued)' 能否支持 继续阅读的链接,我试了下没成功,谢谢!

#19 楼 @pobing 你试试直接传入一段 HTML 字符串看看 我是把 omission 置为空,然后到页面上手工添加链接的 HTML

#20 楼 @edokeh 中文截取不显示问题解决了,在 .rvm/gems/ruby-2.0.0-p0/gems/truncate_html-0.9.2/lib/truncate_html/html_string.rb 修改 为 add \p{P}

private
   def regex
     /(?:<script.*>.*<\/script>)+|<\/?[^>]+>|[[[:alpha:]]\w\|`~!@#\$%^&*\(\)\-_\+=\[\]{}:;'",\.\/?]+|\s+|[[:punct:]]+|\P{P}/ #add \p{P}
   end

看了你的博客不错,你的搜索用 google 搜索?哪里有 api ? 具体我没找到

#21 楼 @pobing 呵呵,没用 API,就是直接拼接了 Google 的搜索 URL

<form action="http://google.com/search">
      <input type="text" name="q">
      <input type="hidden" name="q" value="site:chaoskeh.com">
</form>

#22 楼 @edokeh 这么简单啊,呵呵,不过这样是否需要 google 收录你的网站啊,没爬虫到的是不是就搜不到了?

#23 楼 @pobing 哦对,貌似我去 google 上提交了网站的,不过时间久了细节不记得了。。。

<%= truncate(strip_tags(raw(item.topic_content)),:length=>300) %> 先将 html 标签用 strip_tags 过滤 然后再用 truncate 截取想要的长度字数即可~

需要 登录 后方可回复, 如果你还没有账号请 注册新账号