我一直在尝试让 Rails 的祖先 gem 工作,但我似乎无法实现即使是最简单的情况。我是 Rails 新手,所以我很容易犯一个菜鸟错误。
我试图实现的只是一个普通的评论系统,其中对评论的回复将使用祖先的子/父功能显示在同一页面上。
下面是我的显示 html,它有一个回复链接,我正在尝试设置父子关系:
<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 %>
以下是一些控制器方法:
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
我似乎无法与上述代码建立任何父/子关系