15
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

無限スクロール(InfiniteScroll)をKaminari(Rails)で実装

Posted at

なんかInfiniteScrollがversion3に新しくなった?っぽい感じがして、あまり最新の実装方法を書いている記事が見当たらなかった。

ここでダウンロードするJSファイルも新しい名前のファイルになったのか、実装例がなかった。

# Gemfile

# スクロール読み込み
gem 'kaminari'
gem 'jquery-turbolinks'

Gemを読み込み bundle i

Viewを編集。今回はpartialの内容を読み込むように設定。Slimがわからない人は、Slimでググってください。

/ view html.slim

# jopost
  = render partial: 'jo', collection: @jo
= paginate @jo
/ view partial _jo.html.slim
.jo
  p 無限スクロールの内容

Controllerも単純。

# Controller

  def show
    @jo = Jo.page(params[:page])
  end

最後に、上述のjsをセットして、4行の単純なコードを足せばok。

# assetsのコントローラー.coffee

# スクロール読み込み
//= require "infinite-scroll.pkgd.min"

# 無限スクロール
$('#jopost').infiniteScroll
  path: '.next a'
  append: '.jo'
  history: false

簡単だなぁ。

15
15
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?