【发布时间】: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