简体   繁体   中英

How do I spread my array search result over multiple pages in Rails?

In Rails 4, I have a search query that returns and displays user records in pairs. So, when the search is performed, my controller returns its results to a dedicated search results page in a 2D array, like this:

[[record1, record2],[record3, record4]...]

I'd like to display a record pair per page . Right now, I can only do that with one pair, and it's a pain. My HTML looks something like this:

<% r = @records[0][0] if !@records[0][0].nil? %>            
    <div class = "one-record">
        <%= image_tag r.photo, class: "record-photo" %>
        <p class = "caption"><%= u.name %>: <%= r.caption %></p>
    </div>
<% r = @records[0][1] if !@records[0][1].nil? %>            
    <div class = "one-record">
        <%= image_tag r.photo, class: "record-photo" %>
        <p class = "caption"><%= u.name %>: <%= r.caption %></p>
    </div>
  1. I'm repeating the same exact HTML - is there a way to handle that?
  2. I want to have a "Next >>" button that allows me to pull up @records[1][0] and @ records[1][1] , and so on. How do I handle my routing? Could I only render the "search results part of the page? Is there a standard way of handling something like this?

The will_paginate gem may provide a solution for you. There is a default number of records displayed per page but you can override that.

This topic is called "pagination," and there are gems that help with this. I ended up using the Kaminari gem which has nearly identical functionality to the more popular will_paginate . There's a cool Railcast on this, too: #254

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM