哪个页面有记录?

时间:2016-07-31 12:38:23

标签: ruby-on-rails pagination kaminari

我希望我的寻呼机可以在页面上启动给定记录,并且能够在集合中之前和之后翻阅记录。

我从这里得到代码

https://github.com/amatsuda/kaminari/issues/205及其工作

当我放       <%= @ page_num%> 在show.html.erb上 它给我记录的页码

但我不知道如何让它在

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

这是我的模特 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)) %>');

编辑1: 问题是当我输入记录的页面编号为3时,

http://i.imgur.com/hmgzft6.png

当我尝试导航到页面N 2仍然有效但当我点击第1页时它会回到3页

0 个答案:

没有答案