-
-
Save 3014zhangshuo/e4d14266aa22455d70f905f2b4886b21 to your computer and use it in GitHub Desktop.
Rails - Load more with jQuery and Ajax Code snippets - Tutorial link http://josephndungu.com/tutorials/rails-load-more-results-using-jquery-ajax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="record" data-id="<%= user.id %>"> | |
<p><b>ID: </b> <%= user.id %></p> | |
<p><b>Name: </b> <%= user.name %></p> | |
<p><b>Location: </b> <%= user.location %> </p> | |
<p><%= link_to 'Show', user %> | <%= link_to 'Edit', edit_user_path(user) %> | <%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %> </p> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="container"> | |
<%= render @users %> | |
</div> | |
<div class="load-more-container"> | |
<!-- hide our loading gif image so that we show it when making ajax call via jquery --> | |
<%= image_tag "ajax-loader.gif", style: "display:none;", class: "loading-gif" %> | |
<%= link_to "Load More", users_path, class: "load-more" %> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// append the users partial to our div with class of container | |
$('.container').append('<%= escape_javascript(render(:partial => @users)) %>') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
default_scope { order('created_at DESC') } | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// when the page is ready for manipulation | |
$(document).ready(function () { | |
// when the load more link is clicked | |
$('a.load-more').click(function (e) { | |
// prevent the default click action | |
e.preventDefault(); | |
// hide load more link | |
$('.load-more').hide(); | |
// show loading gif | |
$('.loading-gif').show(); | |
// get the last id and save it in a variable 'last-id' | |
var last_id = $('.record').last().attr('data-id'); | |
// make an ajax call passing along our last user id | |
$.ajax({ | |
// make a get request to the server | |
type: "GET", | |
// get the url from the href attribute of our link | |
url: $(this).attr('href'), | |
// send the last id to our rails app | |
data: { | |
id: last_id | |
}, | |
// the response will be a script | |
dataType: "script", | |
// upon success | |
success: function () { | |
// hide the loading gif | |
$('.loading-gif').hide(); | |
// show our load more link | |
$('.load-more').show(); | |
} | |
}); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsersController < ApplicationController | |
before_action :set_user, only: [:show, :edit, :update, :destroy] | |
# GET /users | |
# GET /users.json | |
def index | |
# if the id params is present | |
if params[:id] | |
# get all records with id less than 'our last id' | |
# and limit the results to 5 | |
@users = User.where('id < ?', params[:id]).limit(5) | |
else | |
@users = User.limit(5) | |
end | |
respond_to do |format| | |
format.html | |
format.js | |
end | |
end | |
....... | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment