【问题标题】:will_paginate does not work in partialwill_paginate 部分不起作用
【发布时间】:2013-12-09 11:34:19
【问题描述】:

我正在制作带有视频的网站。每个视频都可以在 show.html.erb 中进行评论。因为 cmets 的数量可能很大,所以我使用 will_paginate 为 cmets 进行了分页。创建/删除新 cmets 时,我想更新 cmets 列表和分页栏。我无法管理 cmets 和分页以远程更新并正常工作。

我的模型

class Video < ActiveRecord::Base
    attr_accessible :description, :name, :src, :rating, :tag_list
    acts_as_taggable
    belongs_to :user
    has_many :comments

    validates :rating, presence: true, numericality: {:greater_than_or_equal_to => 0, :less_than_or_equal_to => 1000000}
    validates_uniqueness_of :src
    validates :name, presence: true
    validates :src, presence: true

    after_initialize :init

    def init
      self.rating  ||= 0           #will set the default value only if it's nil
    end
  end

我有观看视频/show.html.erb

<p>Name: <%= @video.name %></p>
<p>Description: <%= @video.description %></p>
<div id="comments_form">
<%= render :partial => "comments/comment",
       :collection => @comments %>
<%= will_paginate @comments %>
</div>
<div id="new_comment_form">
<%= render "comments/form" if current_user%>
</div>

我有部分 cmets/_form.html.erb

<%if @user%>
<%= form_for([@video.user, @video, @video.comments.build], remote: true) do |f| %>
<%= render 'shared/error_messages', object: f.object %>   
 <br />
<div class="field">
 <%= f.label "Сообщение" %> 
 <br />
 <%= f.text_field :body %> 
 <br/>
</div>
<div class="actions">
<%= submit_tag "Добавить сообщение", class: "btn btn-large btn-primary" %> 
</div>
<% end %>
<%end%>

我的 cmets/_comment.html.erb

<p>
<%if comment.user%> 
  <b><%= comment.user.name %></b> (создано <%= time_ago_in_words(comment.created_at) %> назад): 
<%end%> 
<%= comment.body %>
<%if comment.user == current_user%>
  <%= link_to "Удалить сообщение", [comment.video.user, comment.video, comment],
                                   :confirm => "Уверен?",
                                   :method => :delete, remote: true %>
  <%end%>

</p> 

我有控制器 controllers/cmets_controller.rb

class CommentsController < ApplicationController
...
def create
    @video=Video.find(params[:video_id])
    @comment = @video.comments.new(params[:comment])
    @comment.user = current_user
    @video=Video.find(params[:video_id]) unless @comment.save
    @comments = @video.comments.paginate(page: params[:page], per_page: 10, order: 'created_at DESC')
    respond_to do |format|
      format.js
    end     
end
 ...
end

我有 cmets\create.js.erb

$("#comments_form").html("<%= escape_javascript(render :partial => "comments/comment",
       :collection => @comments) %><%= escape_javascript(will_paginate @comments) %>")

一切都差不多了:我的新评论已创建,我的分页栏已更新,但链接而不是 /users/1/videos/17?page=1 是 /users/1/videos/17/cmets?page= 1 我努力了两天想知道如何解决这个问题,但没有用......

【问题讨论】:

    标签: jquery ruby-on-rails ruby ruby-on-rails-3 pagination


    【解决方案1】:

    这是因为在视频/cmets#create 中更新了分页栏。默认情况下,分页控制器/动作是当前控制器的索引。您可以在生成分页链接时使用will_paginate params option 强制控制器/动作。

    TBH,我会考虑使用videos/cmets#index 来列出cmets 而不是videos#show。

    【讨论】:

    • 谢谢。我使用videos/cmets#index 列出并在cmets#index 中渲染了'render template: "videos/show"',一切都很好。非常感谢您的快速回复!
    猜你喜欢
    • 1970-01-01
    • 2016-09-18
    • 2016-04-20
    • 2014-07-15
    • 2012-06-16
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多