在可嵌入 javascript 视图中使用mislav_will_paginate
尝试创建一个视图,当嵌入(通过 js)到另一个文档中时,该视图可以提供一组分页结果。这代替了使用 iframe。父文档中的嵌入代码如下所示:
<script type="text/javascript" src="http://MYWEBSITE/search?query=MYPARAMETERS&embed=true&per_page=20"/>
响应此操作的视图 (embed.html.erb) 如下所示:
(function(){
document.write('<div id="df_search_results">')
<% @results.each do |result| %>
document.write('<a href="<%= result.url %>" rel="lightbox[aj]" title="<%= result.title %>"><img src="<%= result.other_url %>" width="100" height="100" /></a>')
<% end %>
<% if @hits > @results.size %>
document.write(<%= will_paginate @results %>)
<% end %>
document.write('</div>')
})()
当排除 will_paginate 标记时,这将按预期工作。当包含 will_paginate 标记时,它无法完全呈现,并且不会在日志中放置任何错误。
Trying to create a view that can serve up a set of paginated results when embedded (through js) in another document. This is in lieu of using an iframe. The embed code in the parent document looks like this:
<script type="text/javascript" src="http://MYWEBSITE/search?query=MYPARAMETERS&embed=true&per_page=20"/>
The view (embed.html.erb) that responds to this action looks like this:
(function(){
document.write('<div id="df_search_results">')
<% @results.each do |result| %>
document.write('<a href="<%= result.url %>" rel="lightbox[aj]" title="<%= result.title %>"><img src="<%= result.other_url %>" width="100" height="100" /></a>')
<% end %>
<% if @hits > @results.size %>
document.write(<%= will_paginate @results %>)
<% end %>
document.write('</div>')
})()
This works as expected when the will_paginate tag is excluded. It fails to render entirely, and does not put any errors in the log, when the will_paginate tag is included.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是这一行:
will_paginate
方法的输出将插入到您的脚本中,但未正确引用为传递给write()
方法的字符串。你需要逃避它。在 Rails 中,您可以使用
escape_javascript method
:(
注意单引号。)
如果您不使用 Rails,您可以 在此处复制其源代码。
The problem is this line:
The output of
will_paginate
method will be inserted in your script but not properly quoted as string passed to thewrite()
method. You need to escape it.In Rails, you can use the
escape_javascript
method:(Notice the single quotes.)
If you don't use Rails, you can copy its source here.