• <bdo id='ECHdE'></bdo><ul id='ECHdE'></ul>

    <small id='ECHdE'></small><noframes id='ECHdE'>

  • <tfoot id='ECHdE'></tfoot>
    <i id='ECHdE'><tr id='ECHdE'><dt id='ECHdE'><q id='ECHdE'><span id='ECHdE'><b id='ECHdE'><form id='ECHdE'><ins id='ECHdE'></ins><ul id='ECHdE'></ul><sub id='ECHdE'></sub></form><legend id='ECHdE'></legend><bdo id='ECHdE'><pre id='ECHdE'><center id='ECHdE'></center></pre></bdo></b><th id='ECHdE'></th></span></q></dt></tr></i><div id='ECHdE'><tfoot id='ECHdE'></tfoot><dl id='ECHdE'><fieldset id='ECHdE'></fieldset></dl></div>
  • <legend id='ECHdE'><style id='ECHdE'><dir id='ECHdE'><q id='ECHdE'></q></dir></style></legend>

      1. 使用 will_paginate gem 在 Rails 3.2.3 中调用 Ajax

        Ajax call in rails 3.2.3 with will_paginate gem(使用 will_paginate gem 在 Rails 3.2.3 中调用 Ajax)

        • <i id='EXx07'><tr id='EXx07'><dt id='EXx07'><q id='EXx07'><span id='EXx07'><b id='EXx07'><form id='EXx07'><ins id='EXx07'></ins><ul id='EXx07'></ul><sub id='EXx07'></sub></form><legend id='EXx07'></legend><bdo id='EXx07'><pre id='EXx07'><center id='EXx07'></center></pre></bdo></b><th id='EXx07'></th></span></q></dt></tr></i><div id='EXx07'><tfoot id='EXx07'></tfoot><dl id='EXx07'><fieldset id='EXx07'></fieldset></dl></div>
            <bdo id='EXx07'></bdo><ul id='EXx07'></ul>

            <small id='EXx07'></small><noframes id='EXx07'>

            <tfoot id='EXx07'></tfoot>
          • <legend id='EXx07'><style id='EXx07'><dir id='EXx07'><q id='EXx07'></q></dir></style></legend>

                    <tbody id='EXx07'></tbody>
                  本文介绍了使用 will_paginate gem 在 Rails 3.2.3 中调用 Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Im trying to implement an Ajax call with the will_paginate gem, I found this guide http://ramblinglabs.com/blog/2011/11/rails-3-1-will_paginate-and-ajax which seemed like a simple solution, though it includes coffeescript which i am not familiar with, so if anyone has a different solution then please advise..

                  My code is as follows

                  My View

                  <div class="container">
                   <div class="row">
                    <div id="userRecipes">
                     <%= render partial: 'userrecipes' %>
                   </div>
                  </div><!--/row-->
                  

                  My partial (userrecipes)

                   <% @recipes.each do |r| %>
                    <div class="span3">
                     <div class="thumbnail">
                      <%= image_tag r.avatar.url(:myrecipes) %>
                     </div>
                     <h4><%= link_to r.dish_name, r %></h4>
                     <hr>
                      <p><%= truncate r.description, :length => 90 %></p>
                      <p><%= link_to "Edit Recipe", edit_recipe_path(r.id) %></p>
                      <p><%= link_to "Delete Recipe", recipe_path(r.id), :confirm => "Are you sure?", :method => :delete %></p>
                      <p><%= link_to "Add to favorites",  {:controller => 'favourites', :action => 'create', :recipe_id => r.id}, {:method => :post } %></p>
                     </div><!--/span3-->
                     <% end %>
                     <%= will_paginate @recipes %>
                  

                  updated userrecipes.js.erb file

                  $('#userRecipes').html('<%= escape_javascript(render partial: 'userrecipes') %>');
                  $.setAjaxPagination();
                  

                  Coffeescript

                  $ ->
                  $.setAjaxPagination = ->
                  $('.pagination a').click (event) ->
                    event.preventDefault()
                    loading = $ '<div id="loading" style="display: none;"><span><img src="/assets/loading.gif" alt="cargando..."/></span></div>'
                    $('.other_images').prepend loading
                    loading.fadeIn()
                    $.ajax type: 'GET', url: $(@).attr('href'), dataType: 'script', success: (-> loading.fadeOut -> loading.remove())
                    false
                  
                    $.setAjaxPagination()
                  

                  When i click on the next anchor tag to show the next set of results the page stays as it is and no new content appears

                  When using the console to see if there are any errors i can see any, the output is

                  GET http://localhost:3000/my_recipes?page=2&_=1355055997639
                  

                  Am i missing something here? or is there an issue with my userrecipes.js.erb file because in other Ajax examples i have seen thy are using escape_javascript when rendering the partial?

                  Edit

                  Whilst inspecting the response in the console it is also showing that the new recipes to be loaded are being loaded but nothing is happening in the view

                  Any pointers appreciated

                  Thanks

                  解决方案

                  Why not try a simpler approach, create a new helper (ex. app/helpers/will_paginate_helper.rb) with the following content:

                  module WillPaginateHelper
                    class WillPaginateJSLinkRenderer < WillPaginate::ActionView::LinkRenderer
                      def prepare(collection, options, template)
                        options[:params] ||= {}
                        options[:params]['_'] = nil
                        super(collection, options, template)
                      end
                  
                      protected
                      def link(text, target, attributes = {})
                        if target.is_a? Fixnum
                          attributes[:rel] = rel_value(target)
                          target = url(target)
                        end
                  
                        @template.link_to(target, attributes.merge(remote: true)) do
                          text.to_s.html_safe
                        end
                      end
                    end
                  
                    def js_will_paginate(collection, options = {})
                      will_paginate(collection, options.merge(:renderer => WillPaginateHelper::WillPaginateJSLinkRenderer))
                    end
                  end
                  

                  Then in your view use this tag for ajax pagination:

                  <%= js_will_paginate @recipes %>
                  

                  Remember that the pagination links will include existing params of the url, you can exclude these as shown below. This is standard will paginate functionality:

                  <%= js_will_paginate @recipes, :params => { :my_excluded_param => nil } %>
                  

                  Hope that solves your problem.

                  Explanation:

                  Will_paginate allows you to create your own custom renderer. The WillPaginateJSLinkRenderer is such a custom renderer and this class could be defined anywhere, it doesn't have to be defined inside the helper module. The custom renderer extends the standard renderer (LinkRenderer) and redefines only two methods.

                  The prepare method is overriden to explicitly remove the cache buster parameter since will_paginate creates the page urls with all parameters that were present when rendering the page and we do not want to reuse the cache buster parameter.

                  The link method is a copy paste from the original LinkRenderer source code but creates a link with remote: true to make it a JS resquest.

                  Finally the js_will_paginate method is a standard view helper method to call the normal will_paginate view helper method but adds our custom renderer to the options so that it will be used instead of the normal renderer.

                  这篇关于使用 will_paginate gem 在 Rails 3.2.3 中调用 Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)
                  ExecJS::ProgramError: SyntaxError: Reserved word quot;functionquot;(ExecJS::ProgramError: SyntaxError: 保留字“function)
                  Infinite scroll and will_paginate appending the #39;next page#39; of items multiple times(无限滚动和 will_paginate 多次附加项目的“下一页)
                  Set of CoffeeScript/JavaScript classes and methods available to rest of Rails app(一组可供 Rails 应用程序使用的 CoffeeScript/JavaScript 类和方法)
                  Attach or embed node add form with a view(使用视图附加或嵌入节点添加表单)
                    1. <legend id='hV0Wb'><style id='hV0Wb'><dir id='hV0Wb'><q id='hV0Wb'></q></dir></style></legend>

                      <i id='hV0Wb'><tr id='hV0Wb'><dt id='hV0Wb'><q id='hV0Wb'><span id='hV0Wb'><b id='hV0Wb'><form id='hV0Wb'><ins id='hV0Wb'></ins><ul id='hV0Wb'></ul><sub id='hV0Wb'></sub></form><legend id='hV0Wb'></legend><bdo id='hV0Wb'><pre id='hV0Wb'><center id='hV0Wb'></center></pre></bdo></b><th id='hV0Wb'></th></span></q></dt></tr></i><div id='hV0Wb'><tfoot id='hV0Wb'></tfoot><dl id='hV0Wb'><fieldset id='hV0Wb'></fieldset></dl></div>

                        <small id='hV0Wb'></small><noframes id='hV0Wb'>

                            <tbody id='hV0Wb'></tbody>

                            <bdo id='hV0Wb'></bdo><ul id='hV0Wb'></ul>
                            <tfoot id='hV0Wb'></tfoot>