Rails 3/Kaminari/JQuery - 加载更多按钮:显示结果的问题(它加载整个页面布局,而不仅仅是部分布局)
从ajax和jquery结合rails开始,我尝试在用户索引页面的底部创建一个“加载更多”链接。 我的用户索引页面应显示前 10 个用户,然后单击链接,将在用户 div 底部加载接下来的 10 个用户,等等。 我使用 Kaminari 进行分页。
我从以下代码开始:
User_controller.rb
def index
@users = User.page(params[:page]).per(10)
end
User/index.html.erb
<div id="users">
<%= render :partial => @users %>
</div>
<%= link_to "Load more", "#", :class => "next" %>
_user.html.erb
<div id="box">
<%= link_to full_name(user), user %>
</div>
application.js
我找到了这个片段(信用:here)尝试满足我的需要:
function loadMore(pageNo) {
var url = '/users?page=';
$.get(url + pageNo, function(response) {
$("#users").append(response);
});
}
$(document).ready(function() {
var currPage = 1;
$("a.next").click(function() {
loadMore(++currPage);
});
});
我工作得很好,除了它加载所有页面布局接下来的结果不仅仅是我的 _user 部分中的框 div 我该如何处理这个问题?我是否必须创建一个index.js.erb(类似于:page.insert_html :bottom, :users, :partial => @users,但在jquery中,如果是这样我该怎么做)?
感谢您的帮助
Begin with ajax and jquery combined with rails, I am trying to create a "load more" link in the bottom of my user index page.
My user index page should display the 10 first users, then clicking the link it loads the next 10 users on the bottom of the user div, etc..
I use Kaminari for pagination.
I started with the following code:
User_controller.rb
def index
@users = User.page(params[:page]).per(10)
end
User/index.html.erb
<div id="users">
<%= render :partial => @users %>
</div>
<%= link_to "Load more", "#", :class => "next" %>
_user.html.erb
<div id="box">
<%= link_to full_name(user), user %>
</div>
application.js
I found this snippet (credit: here) trying to fit it with my need:
function loadMore(pageNo) {
var url = '/users?page=';
$.get(url + pageNo, function(response) {
$("#users").append(response);
});
}
$(document).ready(function() {
var currPage = 1;
$("a.next").click(function() {
loadMore(++currPage);
});
});
I works perfectly except it loads all the page layout with the next results not only the box div in my _user partial
How can I manage this? Did I have to create an index.js.erb (something like: page.insert_html :bottom, :users, :partial => @users but in jquery, if so how can I do it)?
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要为其创建另一个操作。我会使用不显眼的 JavaScript。尝试这样的内容:
User_controller.rb
您不需要在此处更改任何内容,只需确保控制器响应 js
User/index.html.erb
User/index.js.erb
这应该是让它工作所需的全部内容。当然,您可以添加一些 javascript 以在单击链接或其他方式时插入加载图标。 注意:我还没有测试过这个确切的实现,所以可能会有一些错误,但你应该能够明白这个想法
You shouldn't need to create another action for it. I would use unobtrusive javascript. Try something like this:
User_controller.rb
you shouldn't need to change anything here, just make sure the controller responds to js
User/index.html.erb
User/index.js.erb
That should be all that is needed to get it working. Of course you can add some javascript to insert a loading icon when the link is clicked or whatever. Note: I haven't tested this exact implementation, so there may be some errors, but you should be able to get the idea
您应该为此创建一个自己的操作,与其自己的视图相关联。然后,请确保禁用布局渲染:
You should create an own action for this, associated with it's own view. Then, be sure to disable rendering of the layout: