0

I want to have my pager start on the page for a given record and be able to page through the records before and after in the collection.

i get the code from here

https://github.com/amatsuda/kaminari/issues/205 and its working

when i put <%= @page_num %> on show.html.erb it give me the page number that the record is on

but i dont know how i can make it work in

     @page_num = @song.page_num(:per => 10)
     @songs = @artist.songs.page(params[:page] || @page_num ).per(10)

here is my model song.rb

class Song < ActiveRecord::Base

belongs_to :artist

extend FriendlyId
friendly_id :title, use: :slugged
friendly_id :title, :use => :scoped, :scope => :artist


def page_num(options = {})
    column = options[:by] || :id
    order  = options[:order] || :asc
    per    = options[:per] || self.class.default_per_page

    operator = (order == :asc ? "<=" : ">=")
(self.class.where("#{column} #{operator} ?", read_attribute(column)).order("#{column} #{order}").count.to_f / per).ceil
  end

end

songs_controller.rb

class SongsController < ApplicationController
  def show
    @artist = Artist.friendly.find(params[:artist_id])
    @song = @artist.songs.friendly.find(params[:id])
    @page_num = @song.page_num(:per => 10)
    @songs = @artist.songs.page(params[:page] || @page_num ).per(10)
       respond_to do |format|
           format.html
           format.js { render :layout => false }
       end
  end
end

show.html.erb

<div id="songs-table">
  <%= render 'songs' %>
</div>

<div id="songs-paginator" class="text-center">
  <%= paginate @songs, :remote => true %>
</div>

_songs.html.erb

<% @songs.each do |song| -%>
<%= link_to song.title, artist_song_path(@artist, song), class: "list-group-item#{' active' if @song==song}"  %>
<% end -%>

show.js.erb

 $('#songs-table').html('<%= escape_javascript(raw(render 'songs')) %>');
   $('#songs-paginator').html('<%= escape_javascript(paginate(@songs, remote: true)) %>');

edit 1 : the problem is when i enter to record that have the page number 3 it work

https://i.stack.imgur.com/zi6Ix.png

when i try to navigate to the page N 2 still work but when i click on page 1 it go back to the 3 page

anouar
  • 125
  • 1
  • 1
  • 10
  • What's the problem? You want to modify the pagination view? – Tan Nguyen Jul 31 '16 at 14:21
  • he problem is when i enter to record that have the page number 3 it work http://i.imgur.com/hmgzft6.png but when i try to navigate to the page N 2 still work but when i click on page 1 it go back to the 3 page – anouar Aug 01 '16 at 11:44

0 Answers0