论坛首页 入门技术论坛

关于Rails开发之数据排序问题

浏览 2369 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-09-16  

我做一个文章发表的系统,数据库中有表posts(id,title,content).

controller中定义了文件:post_controller.rb,其中有方法:

ruby 代码
  1. def list   
  2.    @post_pages@posts = paginate :posts:per_page => 5   
  3.  end  

在Views中的 list.rhtml中通过以下代码显示:

ruby 代码
  1. <% for post in @posts %>   
  2.     <h3>   
  3.     <%=post.title %>   
  4.     </h3>   
  5.     <%=(post.content)[0..200] %>   
  6.     <br/>   
  7.     <%= link_to 'Show', :action => 'show', :id => post %>   
  8.     <%= link_to 'Edit', :action => 'edit', :id => post %>   
  9.     <%= link_to 'Destroy', { :action => 'destroy', :id => post }, :confirm => 'Are you sure?', :method => :post %>   
  10. <hr/>   
  11. <% end %>  

 

我希望的是数据表中的数据逆序输出,怎么办呢?

   发表时间:2007-09-17  
ruby 代码
  1. def list       
  2.    @post_pages@posts = paginate :posts:per_page => 5  ,:order=>'id DESC'   
  3.  end    




0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics