You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails博客教程报错:undefined method `comments' for Article实例

Ruby on Rails: Article实例调用comments方法触发NoMethodError

我正在开发一个Ruby on Rails项目,此前所有功能都正常运行,但在跟着教程实现评论功能(第8步)时遇到了报错。具体错误为NoMethodError,提示Article实例不存在comments方法。我已经检查了路由配置,也反复核对了教程中的代码,问题依然存在。

以下是我的相关代码文件内容,同时附上页面报错截图,希望大家帮忙找出问题原因:

模型代码

app/models/article.rb

# 粘贴你的Article模型代码

app/models/comment.rb

# 粘贴你的Comment模型代码

Schema文件

db/schema.rb

# 粘贴schema.rb中articles和comments表的相关配置

控制器代码

app/controllers/articles_controller.rb

# 粘贴ArticlesController中相关动作(如show)的代码

app/controllers/comments_controller.rb

# 粘贴CommentsController的代码

视图代码

调用comments方法的视图文件(如app/views/articles/show.html.erb)

# 粘贴视图中调用@article.comments的代码片段

路由配置

config/routes.rb

# 粘贴routes.rb的相关配置

报错截图

报错页面截图

内容的提问来源于stack exchange,提问作者Mael Paquette

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 17:03:10