有没有办法修复 Kaminari 分页生成的 url? [英] Is there any possible way to fix the url that Kaminari pagination generates?

查看:35
本文介绍了有没有办法修复 Kaminari 分页生成的 url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序实现一个聊天系统,其中只有评论列表会通过 ajax 提交重新加载.

Kaminari 分页 在那里使用,但在提交新评论后,它的 url 中出现了像这样的奇怪字符串.

<块引用>

example-website.com/users/mike/refresh_part?_=1356906069855&page=2

当评论在另一个控制器中提交时,它在其 url 中获得更多奇怪的参数参数.只有在遇到垃圾邮件错误时生成的网址:

<块引用>

example-website.com/shop/walmart/topic/20/comments?authenticity_token=9nUyEQ%2Fa0F114vUe16RXf7jhsPw%2B736E%2BKyZFjiWbkQ%3D&comment[body]=Create+Commit&commit&commit;utf8=✓

我该如何解决这个问题?

我的代码是

views/users/show.html.erb

<%= javascript_tag do %>jQuery(文档).ready(函数(){刷新部分();setInterval(refreshPartial, 5000)});函数 refreshPartial() {$.ajax({url: "<%= show_user_path(@user) %>/refresh_part",类型:获取",数据类型:脚本",});}<%结束%>......<span id="聊天"><%=呈现'用户/评论'%></span><%= 渲染 'users/comment_input' %>

views/users/_comment.html.erb

<tr><th>ID</th><th>PIC</th><th>身体</th><th>主题</th><th>发布者</th><th>删除</th></tr><% @comments.each 做 |comment|%><tr id="<%= dom_id(comment) %>"><td><%= comment.id %></td><td><% if comment.comment_icon?%><ul class="缩略图"><%= image_tag(comment.comment_icon.url(:thumb),:height => 100, :width => 100, :style =>'border:3px double #545565;') %><%结束%></td><td><%= comment.body %></td><td><%= comment.subject %></td><td><%= comment.user.user_profile.nickname if comment.user.user_profile %></td><td><%= button_to 'destroy', polymorphic_path([@user, comment]), :data =>{:确认=>'你确定吗?'}, :method =>:delete, :disable_with =>'删除...', :remote =>真,:class =>'btn btn-danger' 如果 current_user &&current_user.id == comment.user_id %></td></tr><%结束%>
<%= 分页 @comments, :window =>4、:outer_window =>5, :left =>2, :right =>2%>

views/users/_comment_input.html.erb <= 这是输入表单!!!!!!

<%=form_for(([@user, @comment]), :remote => true) do |f|%><div class="field"><%= f.label :body %><br/><%= f.text_field :body %>

<div class="field"><%= f.file_field :comment_icon %>

<div class="actions"><%= f.submit %>

<%结束%>

comments_controller.rb

def 创建可评论 = @community_topic||@community||@user@comments = commentable.comment_threads.order("updated_at DESC").page(params[:page]).per(5)@comment = Comment.build_from(commentable, current_user.try(:id), params[:comment][:body])@comment.comment_icon = params[:comment][:comment_icon]如果@user@following_users = @user.all_following(order: 'updated_at DESC')@followed_users = @user.followers@communities_user = @user.get_up_voted(Community).order("updated_at ASC").page(params[:page]).per(5)elif@community结尾last_comment = Comment.where(:user_id => current_user.id).order("updated_at").lastif last_comment &&(Time.now - last_comment.updated_at) <= 10.secondflash[:notice] = "你不能发送垃圾邮件!"渲染:模板=>template_for(可评论)elsif @comment.save#if @community_topic.empty?@comments = commentable.comment_threads.order("updated_at DESC").page(params[:page]).per(5)@comment = commentable.comment_threads.buildresponse_to do |格式|format.html { redirect_to [@community, commentable].uniq, :notice =>评论加了!"}format.js 做如果@community.present?渲染社区/刷新部分"elsif @community_topic.present?渲染'community_topics/refresh_part'elsif @user.present?渲染用户/刷新部分"结尾结尾结尾别的渲染:模板=>template_for(可评论)结尾结尾

views/users/refresh_part.js.erb

$('#chat').html("<%= j(render(:partial => 'users/comment')) %>")

解决方案

这是 Kaminari 的一个已知问题.请参阅:问题 #132问题 #182.

目前已知的解决方法是在分页助手上手动设置参数,例如:

分页@comments, :params =>{:控制器=>'评论', :action =>'index', _: nil, _method: nil,authenticity_token: nil, utf8: nil}

您必须调整该代码以适应您的情况,并确保在手动设置所有参数时测试所有其他功能是否按预期工作.

由于您特别遇到 URL 上的时间戳问题,您应该对问题 132 发表评论,并在那里发布您的经历的详细信息.您参与 github 问题的次数越多,您、项目用户和维护者就越有可能找到更好的解决方案.

I was implementing a chat system to my app, in which only the list of comments will be reloaded with ajax submit.

Kaminari pagination is used in there, but it gets weird string in its url like this after new comment was submit.

example-website.com/users/mike/refresh_part?_=1356906069855&page=2

and it gets more strange argument parameters in its url when comment was submit in the other controller. Only when it was macthed with spamming error Generated url:

example-website.com/shop/walmart/topic/20/comments?authenticity_token=9nUyEQ%2Fa0F114vUe16RXf7jhsPw%2B736E%2BKyZFjiWbkQ%3D&comment[body]=test&commit=Create+Comment&page=2&utf8=✓

How can I fix this?

My codes are

views/users/show.html.erb

<%= javascript_tag do %>
    jQuery(document).ready(function () {
        refreshPartial();
        setInterval(refreshPartial, 5000)
    });


    function refreshPartial() {
      $.ajax({
        url: "<%= show_user_path(@user) %>/refresh_part",
        type: "GET",
        dataType: "script",
      });
    }
<% end %>
......
<span id="chat">
<%= render 'users/comment' %>
</span>
<%= render 'users/comment_input' %>

views/users/_comment.html.erb

<table>
  <tr>
    <th>ID</th>
    <th>PIC</th>
    <th>Body</th>
    <th>Subject</th>
    <th>Posted by</th>
    <th>Delete</th>
  </tr>

<% @comments.each do |comment| %>
  <tr id="<%= dom_id(comment) %>">
    <td><%= comment.id %></td>
    <td>
            <% if comment.comment_icon? %>
                <ul class="thumbnails">
                <%= image_tag(comment.comment_icon.url(:thumb),:height => 100, :width => 100, :style => 'border:3px double #545565;' ) %>
                </ul>
            <% end %>

    </td>
    <td><%= comment.body %></td>
    <td><%= comment.subject %></td>
    <td><%= comment.user.user_profile.nickname if comment.user.user_profile %></td>
    <td>
    <%= button_to 'destroy', polymorphic_path([@user, comment]), :data => {:confirm => 'Are you sure?'}, :method => :delete, :disable_with => 'deleting...', :remote => true, :class => 'btn btn-danger' if current_user && current_user.id == comment.user_id %>
    </td>
    </tr>
<% end %>
</table>

<%= paginate @comments, :window => 4, :outer_window => 5, :left => 2, :right => 2 %>

views/users/_comment_input.html.erb <= This is input form!!!!!

<%=form_for(([@user, @comment]), :remote => true) do |f| %>
    <div class="field">
      <%= f.label :body %><br />
      <%= f.text_field :body %>
    </div>
    <div class="field">
    <%= f.file_field :comment_icon %>
    </div>
  <div class="actions">
    <%= f.submit %>
  </div>

<% end %>

comments_controller.rb

def create
  commentable = @community_topic||@community||@user
  @comments = commentable.comment_threads.order("updated_at DESC").page(params[:page]).per(5)
  @comment = Comment.build_from(commentable, current_user.try(:id), params[:comment][:body]) 
  @comment.comment_icon = params[:comment][:comment_icon] 


  if @user
    @following_users = @user.all_following(order: 'updated_at DESC') 
    @followed_users = @user.followers 
    @communities_user = @user.get_up_voted(Community).order("updated_at ASC").page(params[:page]).per(5)
  elsif @community      

  end

  last_comment = Comment.where(:user_id => current_user.id).order("updated_at").last

  if last_comment && (Time.now - last_comment.updated_at) <= 10.second  
    flash[:notice] = "You cannot spam!" 
    render :template => template_for(commentable)
  elsif @comment.save 
    #if  @community_topic.empty?
        @comments = commentable.comment_threads.order("updated_at DESC").page(params[:page]).per(5)
        @comment = commentable.comment_threads.build

        respond_to do |format|
            format.html { redirect_to [@community, commentable].uniq, :notice => "comment added!"  }
            format.js do
                if @community.present?
                  render 'communities/refresh_part' 
                elsif @community_topic.present?
                  render 'community_topics/refresh_part'
                elsif @user.present?
                  render 'users/refresh_part'
                end
            end
        end 
  else
    render :template => template_for(commentable)
  end
end

views/users/refresh_part.js.erb

$('#chat').html("<%= j(render(:partial => 'users/comment')) %>")

解决方案

This is a known issue with Kaminari. See: Issue #132, Issue #182.

The current known workaround is to manually set params on the pagination helper, for example:

paginate @comments, :params => { :controller => 'comments', :action => 'index', _: nil, _method: nil, authenticity_token: nil, utf8: nil}

You'll have to adjust that code to fit your situation, and be sure to test that all other functionality works as expected when you're manually setting all the params.

Since you're specifically experiencing an issue with timestamps on the URL you should comment on Issue 132 and post the details of your experience there. The more you participate in the github issues the more likely that you, the project users, and the maintainers will be able to find a better solution.

这篇关于有没有办法修复 Kaminari 分页生成的 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆