【发布时间】:2023-03-28 02:06:02
【问题描述】:
帖子 #2 有 1 条评论。当我尝试为帖子 #1 创建 first 评论时,我得到
/posts/1/cmets/2
在评论的索引页面上,它为所有帖子列出了相同的 cmets,无论它们的 ID 是什么。
cmets_controller
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.build(comment_params)
@comment.user = current_user
respond_to do |format|
if @comment.save
format.html { redirect_to [@post, @comment], notice: 'It was successfully created.' }
format.json { render action: 'index', status: :created, location: @comment }
else
format.html { render action: 'new' }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
路线
resources :posts do
resources :comments
end
【问题讨论】:
-
请分享
indexCommentsController 的操作。另外,分享帖子和 cmets 的路线。 -
您在哪里设置
@post用于索引操作。另外,分享routes.rb的内容。 -
让我们在聊天chat.stackoverflow.com/rooms/48530/ror讨论这个问题
标签: ruby-on-rails ruby-on-rails-4