在轨3.2.3 will_paginate gem是项目Ajax调用 [英] Ajax call in rails 3.2.3 with will_paginate gem
问题描述
我试着去实现一个Ajax调用与will_paginate gem是项目,我发现这个指南的 http://ramblinglabs.com/blog/2011/11/rails-3-1-will_paginate-and-ajax 这似乎是一个简单的解决方案,但它包含的CoffeeScript这我不熟悉,因此,如果任何人有不同的解决方案,然后请大家指教。
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..
我的code如下:
我的视图
<div class="container">
<div class="row">
<div id="userRecipes">
<%= render partial: 'userrecipes' %>
</div>
</div><!--/row-->
我的部分(userrecipes)
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 %>
更新userrecipes.js.erb文件
updated userrecipes.js.erb file
$('#userRecipes').html('<%= escape_javascript(render partial: 'userrecipes') %>');
$.setAjaxPagination();
CoffeeScript的
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
我失去了一些东西呢?还是有一个问题,我的userrecipes.js.erb文件,因为在其他的Ajax的例子我已经看见你在使用escape_javascript渲染部分的时候?
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?
修改
虽然检查在控制台中它也表明要加载的新配方被加载,但什么也没有发生在视图中的响应
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
pciated任何指针AP $ P $
Any pointers appreciated
感谢
推荐答案
为什么不尝试一个更简单的方法,创建一个新的辅助(如应用程序/佣工/ will_paginate_helper.rb)具有以下内容:
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
然后在您的视图中使用此标记为Ajax分页:
Then in your view use this tag for ajax pagination:
<%= js_will_paginate @recipes %>
记住,分页链接包括URL的现有PARAMS,您可以排除这些如下图所示。这是标准的将分页功能:
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.
说明:
Will_paginate允许你创建你自己的自定义渲染。所述WillPaginateJSLinkRenderer是这样一个定制渲染器和这个类可在任何地方所定义,它没有被该辅助模块中定义。自定义渲染器扩展了标准的渲染器(LinkRenderer),并重新定义了只有两个方法。
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.
在 prepare法重写以显式删除的缓存克星参数因为will_paginate创建的页面URL以呈现页面时是present所有参数,我们不希望重用缓存克星参数。
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.
在链接方法是从原来的一个复制粘贴<一href="https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/view_helpers/link_renderer.rb"相对=nofollow> LinkRenderer源$ C $ C 而造成远程链接:真正使它成为一个JS resquest
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.
最后的 js_will_paginate 方法是标准的视图助手方法来调用正常will_paginate视图助手方法,但增加了我们的自定义渲染器的选项,以便将使用它,而不是正常渲染。
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.
这篇关于在轨3.2.3 will_paginate gem是项目Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!