【问题标题】:Best data structure and design for this feature此功能的最佳数据结构和设计
【发布时间】:2009-03-26 17:50:19
【问题描述】:

在我的 Rails 应用程序中,我有一个名为 Situations 的部分,它基本上是一种情况的文本描述。但是有几个。我希望能够一次只显示一个,并且每个都在自己的页面上(首先是最新创建的),然后在页面底部我有指向旧版和较新的情况。

假设我的代码中有一个@situations 对象,其中包含我想要显示的所有情况。接下来我应该做什么(在控制器和视图中)。

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    我会使用分页 (will_paginate) 并将每页的项目数设置为 1。

    然后您可以使用current_pagenext_page 来制作您的链接。请在此处查看source

    另外,它只是一个标准索引操作,带有will_paginate 所需的更改。

    这个screencast 应该可以让您很好地了解您需要什么,但请注意该插件自创建以来已经发生了一些更改。详情在will_paginate github wiki.

    【讨论】:

    【解决方案2】:

    models/situation.rb

    class Situation < ActiveRecord::Base
      default_scope :order => 'created_at desc'
    end
    

    控制器/situations_controller.rb

    class SituationsController < ActionController::Base
      def index
        @situations = Situation.all
      end
    end
    

    意见/情况/index.html.erb

    <h1>Situations</h1>
    <%= render @situations %>
    

    意见/情况/_situation.html.erb

    <h2><%= situation.name -%></h2>
    <p><%= situation.description -%></p>
    

    【讨论】:

    • 底部的链接呢?每页一种情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    • 2015-11-20
    • 2014-12-31
    • 1970-01-01
    相关资源
    最近更新 更多