2

我希望用户能够对提交的内容发表评论。在每个提交的下方,我希望有一个链接,上面写着“评论(2)”......当您单击此链接时,它会动态加载评论,以及添加新评论的简单表单。当用户提交新评论时,我希望它在列表底部异步加载。

我现在的工作方式如下:

// index.html.erb

<p class="comments_view_toggle">
  <%= link_to_remote("▼ view comments (#{answer.comments.count})", :controller => "comments", :action => "show", :submission => submission, :update => "comment")  %>
</p>

// comments_controller.rb

  def show
    @submission = Submission.find(params[:submission])
    respond_to do |format|
        format.html { redirect_to root_url }
        format.js
    end
  end

// show.rjs

page.insert_html :bottom, :comment, :partial => 'show', :locals => { :submission => @submission }

// _show.html.erb

    <ul id="comment_list">
        <%= render :partial => 'comments/comment', :collection => submission.comments  %>
    </ul>

    <div class="clear"></div>

        <% form_remote_for Comment.new do |f| %>
            <%= hidden_field_tag(:submission_id, answer.id)%>
            <%= hidden_field_tag(:user_id, current_user.id)%>
            <%= f.text_area :message %>
            <%= f.submit "comment", :disable_with => 'commenting...' %>
        <% end %>

我什至还没有处理切换功能的第二部分(隐藏),因为每当我单击链接时,它都会重新加载链接下方的整个页面,而不是只运行部分页面,我不知道为什么。它似乎没有正确传递参数。我对这一切都错了吗?你能为我指出正确的方向吗?

4

1 回答 1

1

您可以使用默认情况下带有 rails 的原型 ajax 库来做到这一点。或者你可能想用 JQuery 来做,

这是执行此操作的一种方法

假设您有以下 html 页面

<div id="submission">
  // your submission details goes here
</div>
//here you will have your link says "comments"
<div id="comments">

</div>

单击“评论”链接后,您可以使用 link_to_remote 加载评论 div 中的所有评论

例如:link_to_remote "查看评论"、:update => "comments"、:url => { :action => "comments"、:id => submit.id }

所以你的控制器看起来像

class SubmissionsController < ApplicationController
    #your other code goes here

   def list
     #code to generate the comments list
     render :partial => 'comments_list', :object => @comments
   end
end

并且您可以有部分列表评论“_comments_list”

于 2010-09-12T08:36:10.570 回答