简体   繁体   English

Ruby on Rails - 不能嵌套注释

[英]Ruby on Rails- Can not nested comment

I use closure_tree gem for my project to nest micropost but it has errors.我在我的项目中使用了closure_tree gem 来嵌套 micropost,但它有错误。 Here is my controller:这是我的控制器:

def index
    @microposts = current_user.microposts.hash_tree
  end

  def new
    @micropost = current_user.microposts.build(parent_id: params[:parent_id])
  end

  def create
    if params[:micropost][:parent_id].to_i > 0
      parent = current_user.microposts.find_by_id(params[:micropost].delete(:parent_id))
      @micropost = parent.children.build(micropost_params) # error in here
    else
      @micropost = current_user.microposts.build(micropost_params)
    end
    if @micropost.save
      flash[:success] = 'Micropost created!'
      redirect_to root_url
    else
      @feed_items = []
      render 'static_pages/home'
    end
  end
private

  def micropost_params
    params.require(:micropost).permit(:content, :picture)
  end
  # Returns the current logged-in user (if any).
  def current_user
    if (user_id = session[:user_id])
      @current_user ||= User.find_by(id: user_id)
    elsif (user_id = cookies.signed[:user_id])
      user = User.find_by(id: user_id)
      if user && user.authenticated?(:remember, cookies[:remember_token])
        log_in user
        @current_user = user
      end
    end
  end
end

I can not reply any micropost.我无法回复任何微博。 First, if I reply myself, error is "User can not be blank".首先,如果我自己回复,错误是“用户不能为空”。 Second, if I reply any user micropost, error is "undefined method `children' for nil:NilClass".其次,如果我回复任何用户微博,错误是“nil:NilClass 的未定义方法`children'”。 Creating micropost is normally.创建微博是正常的。

Firstly, use acts_as_tree - closure_tree is good but not as good, except maybe for the querying (which closure_tree does with 1 select).首先,使用acts_as_tree closure_tree很好,但没有那么好,除了查询( closure_tree用 1 个选择执行)。

-- ——

Anyways, there are quite a few fixes to be made.无论如何,有很多修复要做。 I'll just detail how I'd do it:我将详细说明我将如何做:

#app/models/micropost.rb
class Micropost < ActiveRecord::Base
   has_many :comments
end

#app/models/comment.rb
class Comment < ActiveRecord::Base
   belongs_to :micropost
   acts_as_tree #-> requires parent_id column in table
end 

This will give you the appropriate setup in your models.这将为您的模型提供适当的设置。 Here's how to handle it properly in your controller:以下是在控制器中正确处理它的方法:

#config/routes.rb
resources :microposts do
   resources :comments, only: [:create]
end

#app/controllers/microposts_controller.rb
class MicropostsController < ApplicationController
   def show
      @micropost = Micropost.find params[:id]
      @comment   = @micropost.comments.new
   end 
end

#app/controllers/comments_controller.rb
class CommentsController < ApplicationController
   def create
      @micropost = Micropost.find params[:micropost_id]
      @comment   = @micropost.comments.new comment_params
      redirect_to @micropost if @comment.save
   end

   private

   def comment_params
      params.require(:comment).permit(:title, :body)
   end
end

This will allow you to show the Micropost & Comments in the views:这将允许您在视图中显示MicropostComments

#app/views/microposts/show.html.erb
<%= render @micropost %>

<%= render "comments/new" %>
<%= render @micropost.comments if @micropost.comments.any? %>

#app/views/microposts/_micropost.html.erb
<%= micropost.title %>
<%= micropost.body %>

#app/views/micropost/_comment.html.erb
<%= comment.title %>
<%= comment.body %>
<%= render @micropost.comments.children if @micropost.comments.children.any? %>

Update更新

Thanks for the comment, you'll want to look at Single Table Inheritance if you have exactly the same models:感谢您的评论,如果您有完全相同的模型,您将需要查看Single Table Inheritance

#app/models/micropost.rb
class Micropost < ActiveRecord::Base
   #columns id | type | parent_id | title | body | created_at | updated_at
   has_many :comments
end

#app/models/comment.rb
class Comment < Micropost
   #no table needed
   belongs_to :micropost
   acts_as_tree
end

My above code will work in exactly the same way except a single table will be used now.我上面的代码将以完全相同的方式工作,除了现在将使用单个表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM