###前提・実現したいこと
http://ruby-rails.hatenadiary.com/entry/20141115/1416021886
こちらのサイトを参考にrailsアプリケーションにInfinite Scroll
とkaminari
を使用して無限スクロールの実装をしました。
手順通り1から全て実装してみました。
ですが、最後挙動を確認してみると
このようにページを遷移するリンクが表示されるだけで無限スクロールにはなりませんでした。
このような場合、原因はどこにあるのでしょうか?
どこか間違っている箇所などあれば教えてください。
###該当のソースコード
shops_controller.rb
ruby
1class ShopsController < ApplicationController 2 before_action :set_shop, only: [:show, :edit, :update, :destroy] 3 4 # GET /shops 5 # GET /shops.json 6 def index 7 @shops = Shop.page(params[:page]).per(2) 8 end 9 10 # GET /shops/1 11 # GET /shops/1.json 12 def show 13 end 14 15 # GET /shops/new 16 def new 17 @shop = Shop.new 18 end 19 20 # GET /shops/1/edit 21 def edit 22 end 23 24 # POST /shops 25 # POST /shops.json 26 def create 27 @shop = Shop.new(shop_params) 28 29 respond_to do |format| 30 if @shop.save 31 format.html { redirect_to @shop, notice: 'Shop was successfully created.' } 32 format.json { render :show, status: :created, location: @shop } 33 else 34 format.html { render :new } 35 format.json { render json: @shop.errors, status: :unprocessable_entity } 36 end 37 end 38 end 39 40 # PATCH/PUT /shops/1 41 # PATCH/PUT /shops/1.json 42 def update 43 respond_to do |format| 44 if @shop.update(shop_params) 45 format.html { redirect_to @shop, notice: 'Shop was successfully updated.' } 46 format.json { render :show, status: :ok, location: @shop } 47 else 48 format.html { render :edit } 49 format.json { render json: @shop.errors, status: :unprocessable_entity } 50 end 51 end 52 end 53 54 # DELETE /shops/1 55 # DELETE /shops/1.json 56 def destroy 57 @shop.destroy 58 respond_to do |format| 59 format.html { redirect_to shops_url, notice: 'Shop was successfully destroyed.' } 60 format.json { head :no_content } 61 end 62 end 63 64 private 65 # Use callbacks to share common setup or constraints between actions. 66 def set_shop 67 @shop = Shop.find(params[:id]) 68 end 69 70 # Never trust parameters from the scary internet, only allow the white list through. 71 def shop_params 72 params.require(:shop).permit(:name, :zipcode, :address, :tel) 73 end 74end
index.hetml.erb
ruby
1<p id="notice"><%= notice %></p> 2 3<h1>Listing Shops</h1> 4 5<table id="shops"> 6 <thead> 7 <tr> 8 <th>Name</th> 9 <th>Zipcode</th> 10 <th>Address</th> 11 <th>Tel</th> 12 <th colspan="3"></th> 13 </tr> 14 </thead> 15 16 <tbody class="page"> 17 <%= render @shops %> 18 </tbody> 19</table> 20 21<br> 22 23<%= link_to 'New Shop', new_shop_path %> 24 25<%= paginate @shops %>
_shop.hetml.erb
ruby
1<tr class="shop"> 2 <td><%= shop.name %></td> 3 <td><%= shop.zipcode %></td> 4 <td><%= shop.address %></td> 5 <td><%= shop.tel %></td> 6 <td><%= link_to 'Show', shop %></td> 7 <td><%= link_to 'Edit', edit_shop_path(shop) %></td> 8 <td><%= link_to 'Destroy', shop, method: :delete, data: { confirm: 'Are you sure?' } %></td> 9</tr>
index.js.erb
ruby
1$("#shops").append("<tbody class='page'><%= escape_javascript(render(@posts)) %></tbody>");
shops.coffee
ruby
1$ -> 2 $("#shops .page").infinitescroll 3 loading: { 4 img: "http://www.mytreedb.com/uploads/mytreedb/loader/ajax_loader_blue_48.gif" 5 msgText: "ロード中..." 6 } 7 navSelector: "nav.pagination" # selector for the paged navigation (it will be hidden) 8 nextSelector: "nav.pagination a[rel=next]" # selector for the NEXT link (to page 2) 9 itemSelector: "#shops tr.shop" # selector for all items you'll retrieve
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/15 06:23