【问题标题】:Custom links on Paginate分页上的自定义链接
【发布时间】:2014-11-22 03:48:21
【问题描述】:

我已经实现了 will_paginate,现在我想将默认页面更改为来自 ruby​​ 变量的日期。我该怎么做呢?

更准确地说:我的目标是每个日期有 1 页,并且在此页面上拥有属于该日期的所有数据

视图中的片段:

  def show
    @waves = @location.infos.paginate(page: params[:page], per_page: 7)
    gon.watch.infos = @location.infos
    respond_with(@location)

来自控制器的片段:

<table class= "table table-striped">
            <thead>
                <th><strong>Day</strong></th>
                <th><strong>Swell Size</strong></th>
                <th><strong>Swell Rating</strong></th>
                <th><strong>Wind Speed</strong></th>
                <th><strong>Temperature</strong></th>
            </thead>
            <tbody>
                <!-- <%= will_paginate @waves, previous_label: "Previous day", next_label: "Next day", renderer: BootstrapPagination::Rails %> -->              
                <%= will_paginate(@waves, :renderer => PaginationListLinkRenderer) %>
                <% @waves.each do |wave| %>
                <tr>
                    <td><%= Time.at(wave.day.to_i).to_s.chomp("-0800")%></td>
                    <td><%= wave.size_min%>-<%= wave.size_max%><small>ft</small></td>
                    <td><% swell = wave.swell_rating %>
<td><%= wave.wind_speed%><small>mph</small></td>
                    <td><%= wave.temperature%><small>°F</small>   </td>

【问题讨论】:

    标签: ruby-on-rails ruby pagination


    【解决方案1】:

    在这种情况下,您并没有真正对infos 进行分页。您正在为这些记录分页 created_on 属性。

    所以:

    • 确保这些记录中有created_on
    • 获取按created_on 分组的记录。原始 sql 或范围将完成一项工作。 (虽然我们不会返回正确的对象)
    • 当您有一个created_on 日期数组时,您需要创建某种分页控件来显示这些日期。我认为您也许可以将 will_paginate 用于数组。
    • 加载选定日期范围内的信息:@location.infos.for_date(params[:date])

    基本上,您想要做的事情需要一些肘部油脂。

    【讨论】:

    • 感谢您的回复。因此,我在记录上创建了_on,还有一个名为 day 的属性,它是我想要使用的 unix 时间戳。但是,我在哪里创建 sql 查询(键入什么文件等)和分页控件。你能提供一些示例代码吗?
    猜你喜欢
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 2013-01-13
    • 2020-04-23
    相关资源
    最近更新 更多