在Rails中使用ajax渲染Ancestry嵌套注释的回复表单

时间:2015-12-29 04:41:55

标签: ruby-on-rails ruby ajax

我使用ancestry gem嵌套了注释。 我面临的挑战是如何使用ajax / jquery在同一页面上的相应评论下呈现回复表单,而不是重定向到“新”页面以便键入回复。评论与另一个名为Scoreboard的模型相关联。我到目前为止所做的相应代码文件如下:

记分牌#显示页面,其中包含表单区域:

<div class= "comment-section">

  <%= form_for [@scoreboard, @comment], :html => { :id => "new-comment-entry" } do |f| %> 

       <%= render 'shared/error_messages', object: f.object %>
       <%= f.text_area :body, class: "comment-field" %>
       <%= f.hidden_field :parent_id %>
       <%= f.submit "Join the discussion...",:data => {:disable_with => "Uploading Comment..."}, class: " comment-button btn btn-primary" %>
  <% end %>

  <%= nested_comments @scoreboard.comments.arrange(:order => :created_at) %>
 </div>

_comment.html.erb

<div class=" comment-div"> 
 <p> Posted by <%= link_to "#{comment.user.name}", comment.user %>
   <%= time_ago_in_words(comment.created_at) %> ago
 </p>
 <div class="comment-body" id="comment-<%= comment.id %>">
  <%= comment.body %>
  <%= link_to "Reply", new_scoreboard_comment_path(@scoreboard, comment, parent_id: comment.id), remote: true %> |
  <%= link_to "Delete", scoreboard_comment_path(@scoreboard, comment), :data => {:confirm => 'Delete Message?'}, method: :delete %>
 </div>
</div>

评论控制器新&amp;创建方法

def new
 @scoreboard = Scoreboard.find(params[:scoreboard_id])
 @comment = @scoreboard.comments.new(:parent_id => params[:parent_id])
 respond_to do |format|
     format.js { render action: "new" }
 end
end



def create
     @scoreboard = Scoreboard.find(params[:scoreboard_id])
     @comment = @scoreboard.comments.new comment_params
     respond_to do |format|
      if @comment.save
        format.html { redirect_to scoreboard_url(@comment.scoreboard_id) }
      else
       format.html  { 
        redirect_to scoreboard_url(@comment.scoreboard_id)
        flash[:success] = 'Comment must be less than 140 characters'
       }
      end
     end
end

new.js.erb

$("#comment-<%=@comment.parent_id %>").after("<%= j render "reply_form");

_reply_form.html.erb

<%= form_for [@scoreboard, @comment] do |f| %> 
        <%= render 'shared/error_messages', object: f.object %>
        <%= f.text_area :body, class: "comment-field" %>
        <%= f.hidden_field :parent_id %>   
        <%= f.submit "Reply", class: " comment-button btn btn-primary" %>               
<% end %>

这只是我对它应该如何发生的看法,显然我做错了,因为在开发中我得到以下错误:

Completed 406 Not Acceptable in 114ms

ActionController::UnknownFormat (ActionController::UnknownFormat):
  app/controllers/comments_controller.rb:6:in `new'

如果有的话,我愿意接受不同的方法。提前致谢。

编辑: 如果它可能导致麻烦,请包括nested_comments帮助器方法:

def nested_comments(comments) 
  comments.map do |comment, sub_comment|
    render(comment) + content_tag(:div, nested_comments(sub_comment), class:  "nested_messages")
  end.join.html_safe
end

1 个答案:

答案 0 :(得分:2)

我认为问题在于这一行:

<%= link_to "Reply", new_scoreboard_comment_path(@scoreboard, comment, parent_id: comment.id), remote: true %>

将其更改为

<%= link_to "Reply", new_scoreboard_comment_path(@scoreboard, comment, parent_id: comment.id, js: true), remote: true %>

另外,我认为您的new.js.erb有语法错误。应该是:

$("#comment-<%=@comment.parent_id %>").after("<%= j render('reply_form', scoreboard: @scoreboard, comment: @comment %>");

将reply_form的第一行更改为:

<%= form_for [scoreboard, comment] do |f| %>