论坛的置顶功能是怎么实现的?置顶帖子也可以设置显示顺序,还有加精的帖子高亮显示,这些都好像是一种方法,如果以railscasts的第一个博客例子为基础,需要添加什么才能让博文置顶或者高亮显示?
看了下源码,实现方式如下
view 里面会
<%= render_cell :topics, :sidebar_suggest_topics %>
这个模板位于 app/cells/topics/sidebar_suggest_topics.rb
cells/topics.rb 会给出需要置顶的话题,显示 5 条,按建议时间降序
def sidebar_suggest_topics @suggest_topics = Topic.suggest.limit(5) render end
scope :suggest, where(:suggested_at.ne => nil).desc(:suggested_at)
置顶话题是管理员创建
def suggest @topic = Topic.unscoped.find(params[:id]) @topic.update_attribute(:suggested_at, Time.now) CacheVersion.topic_last_suggested_at = Time.now redirect_to(cpanel_topics_path, :notice => "Topic:#{params[:id]} suggested.") end