【问题标题】:Is there any possible way to fix the url that Kaminari pagination generates?有没有办法修复 Kaminari 分页生成的 url?
【发布时间】:2012-12-16 10:40:27
【问题描述】:

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

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

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

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

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

我该如何解决这个问题?

我的代码是

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

<%=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 %>

cmets_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')) %>")

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 kaminari


    【解决方案1】:

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

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

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

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

    由于您特别遇到了 URL 上的时间戳问题,因此您应该对问题 132 发表评论并在此处发布您的体验详情。您参与 github 问题的次数越多,您、项目用户和维护者就越有可能找到更好的解决方案。

    【讨论】:

    • 非常感谢:) 我尝试了你的建议。但它似乎禁用了 remote=>true :( 你能说出为什么吗?我添加了这个 4, :outer_window => 5, :left => 2, :right => 2, :params => { :controller => 'communities', :action => 'show', _: nil, _method: nil,authenticity_token: nil, utf8: nil} %>
    • 好吧,您只需将:remote =&gt; true 放回paginate 的参数列表中。
    猜你喜欢
    • 2016-02-14
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 2019-04-17
    • 2016-07-09
    • 2020-03-18
    相关资源
    最近更新 更多