Javascript 在RubyonRails中呈现所有项目时隐藏“加载更多”按钮
我目前正在尝试在RubyonRails中实现LoadMore按钮。我成功地实现了它。但是,即使没有更多的照片要渲染,仍会显示“加载更多”按钮 以下是代码列表: index.html.erbJavascript 在RubyonRails中呈现所有项目时隐藏“加载更多”按钮,javascript,ruby-on-rails,ruby,ajax,Javascript,Ruby On Rails,Ruby,Ajax,我目前正在尝试在RubyonRails中实现LoadMore按钮。我成功地实现了它。但是,即使没有更多的照片要渲染,仍会显示“加载更多”按钮 以下是代码列表: index.html.erb <div class = 'container'> <div class = 'section-header'> <h2> Let's Rewind! </h2> </div>
<div class = 'container'>
<div class = 'section-header'>
<h2> Let's Rewind! </h2>
</div>
<div class='row'>
<div class = 'container'>
<div class='cust-cont'>
<%= render @scroll_photos %>
</div>
</div>
</div>
<div class="load-more-container">
<%= image_tag "ajax-loader.gif", style: "display:none;", class: "loading-gif" %>
<%= link_to "Load More", scroll_photos_path, class: "load-more" %>
</div>
</div>
index.js.erb
$('.cust-cont').append('<%= escape_javascript(render(:partial => @scroll_photos)) %>')
当控制器操作返回空@scroll\u photos时,可以隐藏加载更多部分
嗨,谢谢你的回答!由于它包含一个ruby部分,我应该将其添加到index.js.erb中吗?但当我将其添加到文件中时,该部分没有被检查。期待您的进一步答复!它如何检查附加照片的代码,但无法运行此部分。我更新了我的代码
if params[:year]
# get all records with id less than 'our last id'
# and limit the results to 5
@scroll_photos = ScrollPhoto.where('year < ?', params[:year]).limit(2)
else
@scroll_photos = ScrollPhoto.limit(2)
end
respond_to do |format|
format.html
format.js
end
$(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_year = $('.record').last().attr('data-year');
// 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('year'),
// send the last id to our rails app
data: { year: last_year },
// 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();
}
});
});
});
$('.cust-cont').append('<%= escape_javascript(render(:partial => @scroll_photos)) %>')
# in your javascript file
<% if @scroll_photos.empty? %>
$('.load-more-container').hide()
<% else %>
$('.cust-cont').append('<%= escape_javascript(render(:partial => @scroll_photos)) %>')
<% end %>