Rails 3/Kaminari/JQuery - load more button: problem to display results (it loads the entire page layout and not only the partial layout)

后端 未结 2 1625
礼貌的吻别
礼貌的吻别 2020-12-30 17:34

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 d

相关标签:
2条回答
  • 2020-12-30 17:54

    You should create an own action for this, associated with it's own view. Then, be sure to disable rendering of the layout:

    def more_users
      @users = User.page(params[:page]).per(10)
      render :layout => false
    end
    
    0 讨论(0)
  • 2020-12-30 17:56

    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

    respond_to :html, :js
    

    User/index.html.erb

    <div id="users">
      <%= render :partial => @users %>
    </div>
    <%= link_to_next_page @users, 'Load More', :remote => true, :id=>"load_more_link" %>
    <%###  link_to_next_page is a Kaminari function that returns exactly what the name implies %>
    

    User/index.js.erb

    $('#users').append("<%= escape_javascript(render :partial => @users)%>");
    $('#load_more_link').replaceWith("<%= escape_javascript(link_to_next_page(@users, 'Load More', :remote => true, :id=>'load_more_link'))%>");
    

    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

    0 讨论(0)
提交回复
热议问题