gpt4 book ai didi

ruby-on-rails - Ruby on Rails - 无法嵌套评论

转载 作者:数据小太阳 更新时间:2023-10-29 09:02:00 29 4
gpt4 key购买 nike

我在我的项目中使用closure_tree gem 来嵌套微博,但它有错误。这是我的 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

我不能回复任何微博。首先,如果我自己回复,错误是“用户不能为空”。其次,如果我回复任何用户微博,错误是“undefined method ‘children’ for nil:NilClass”。创建微博正常。

最佳答案

首先,使用 acts_as_tree - closure_tree 很好但不是那么好,除了可能用于查询(closure_tree 使用 1 个选择) .

--

无论如何,还有很多修复工作要做。我将详细说明我将如何做:

#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

这将为您的模型提供适当的设置。以下是如何在您的 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

这将允许您在 View 中显示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? %>

更新

感谢您的评论,您会想看看 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

我上面的代码将以完全相同的方式工作,除了现在将使用一个表。

关于ruby-on-rails - Ruby on Rails - 无法嵌套评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33319178/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com