Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Custom links on Paginate

I have implemented will_paginate and I would now like to change the default pages to dates from a ruby variable. How would I go about doing that?

To be a bit more precise: my goal is to have 1 page per date and have all the data that belongs to this date on this page

controller:

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

view

<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>
...

1 Answer

Lisa Rossiter
Lisa Rossiter
3,630 Points

It doesn't seem like will_paginate was built for that kind of functionality. What I would do which is not gospel by a long way is generate a list of dates by splitting out @waves. I think you should be able to chink by date some how. Then list all the dates as links for example waves/20-10-4 then use date_id or something with a where clause and use will_paginate for those results. Perhaps add an index on created_at for extra speed. Sorry if that makes no sense, just say if anything doesn't really make any sense lol.