Question

I've been trying to get the ancestry gem for rails to work but I seem to fail at implementing even the most simplest case. I'm new to rails so I could easily be making a rookie mistake.

What I was trying to implement was just a normal commenting system where replies to comments would be shown all on the same page using ancestry's child/parent functionality.

Below is my show html which has a reply link which is where I am trying to set up the parent child relationship:

 <p id="notice"><%= notice %></p>
<%=@comment.children.nil?%>
<p>
  <strong>Body:</strong>
  <%= @comment.body %>
</p>
<p>
    <%if !@children.nil?%>
    <% @children.each do |c|%>
    c.body
    <%end%>
<%end%>
</p>


<p>
  <strong>Author:</strong>
  <%= @comment.author %>
</p>
<%= link_to 'Reply', new_comment_path, :parent_id => @comment  %>
<%= link_to 'Edit', edit_comment_path(@comment) %> |
<%= link_to 'Back', comments_path %>

And here is some of the controller methods:

class CommentsController < ApplicationController
  before_action :set_comment, only: [:show, :edit, :update, :destroy]

  # GET /comments
  # GET /comments.json
  def index
    @comments = Comment.all
  end

  # GET /comments/1
  # GET /comments/1.json
  def show
    @children=@comment.children
  end

  # GET /comments/new
  def new
  @parent_id = params.delete(:parent_id)
  @comment = Comment.new(:parent_id => @parent_id)
  end
  //omitted code
  private
    # Use callbacks to share common setup or constraints between actions.
    def set_comment
      @comment = Comment.find(params[:id])
    end

I can not seem to get any parent/child relationship with the above code

Était-ce utile?

La solution

ok I figured it out in the end. I was only passing the parent parameter to the "new" controller method, I should have been passing it again after this at the create stage. Was a silly mistake.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top