mardi 24 novembre 2015

Can't get nested comments to work with ancestry gem

I'm trying to implement replies for my comments in rails. I was watching this video as a guideline http://ift.tt/1nN78vl All the comments I create just becomes the root node, everytime I try replying to a comment it seems like it just doesn't register as the children node. (I've checked with rails console and the column 'ancestry' for the replying comment is always nil)

My comment is a nested resources under the Post Model. I suspect the problem is with the create function in the comments controller?

using rails '4.2.4'

Comments controller:

def new
    @post = Post.find(params[:post_id]) 
    @comment = @post.comments.build
    @comment.parent_id = params[:parent_id]

end

def create

    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(comment_params)
    @comment.user = current_user

    if @comment.save
        redirect_to build_post_path(@post)
    else
        redirect_to build_post_path(@post)
    end
end

comments/new.html.erb

    <div class = "row">
        <div class="col-md-8 col-md-offset-2 ">
            <h3>Reply</h3>
            <ol class ="comments">
                <%= render @comment.parent if @comment.parent %>
            </ol>
        </div>
    </div>


    <%= render 'form' %>

comments/_form.html.erb

<div class = "row">
      <div class = "col-md-5 col-md-offset-2">
            <%= form_for([@post, Comment.new]) do |f| %>

                  <%= render 'shared/error_messages',object: f.object %>

                  <%= f.hidden_field :parent_id %>

                  <%= f.label :comment %>
                  <%= f.text_area :comment , class: 'form-control', rows: "4"  %>

                  <div class = "row">
                        <div class = "col-md-3">
                              <%= f.submit "Submit", class: "btn btn-primary" %>
                        </div>
                  </div>
            <% end %>
      </div>
</div>

comments/_comments.html.erb

<li id="comment-<%= comment.id %>">

  <span class="avatar"><%= image_tag(comment.user.avatar.url(:thumb)) %></span>
  <span class="user"><%= link_to comment.user.name, comment.user %> <span class = "timestamp"><%= time_ago_in_words(comment.created_at) %> ago.</span></span>
  <span class="content"><%= comment.comment %></span>
  <span class="options">

    <%= link_to "Index", post_comments_path %> | 

    <%= link_to "Reply", new_post_comment_path(:parent_id => comment) %> |

    <% if current_user?(comment.user) %>
      <%= link_to "Delete", [comment.post, comment], method: :delete,
                                       data: { confirm: "You sure?" } %>
      <%= link_to "Edit", edit_post_comment_path(comment.post, comment) %>
    <% end %>
  </span>

</li>

post/show.html.erb

<div class = "row">
    <div class="col-md-8 col-md-offset-2 ">
        <h3>Comments (<%= @post.comments.count %>)</h3>
        <ol class ="comments">
            <%= nested_messages @post.comments.arrange(:order => :created_at) %>
        </ol>
    </div>
</div>

helper method

def nested_messages(messages)
    messages.map do |message, sub_messages|
      render(message) + content_tag(:div, nested_messages(sub_messages), :class => "nested_messages")
    end.join.html_safe
  end

Aucun commentaire:

Enregistrer un commentaire