Ruby on Rails - 查询传递参数
我正在创建一个简单的 RoR 应用程序,我可以在其中创建帖子并对它们发表评论。我对如何做到这一点的想法是让帖子和评论具有相同的类型并使用相同的 MVC,但评论具有父 ID。帖子的父 ID 为 0,而评论的父 ID 为 0。我在想,当我尝试创建评论时,我可以将父级的 id 传递给新方法。这不起作用。虽然我没有收到错误,但新方法中的 @post 变量似乎与 create 方法中的 @post 变量不同。我的直觉是,新方法创建一个新对象,然后将该对象传递给视图。然后视图填充对象参数,然后将其发送到 create 方法,create 方法将对象保存到数据库。从反复试验来看,情况似乎并非如此,除非我只是做错了。有没有更简单的方法来获得我想要实现的相同功能?或者有什么办法可以让我上班?任何帮助将不胜感激!
I am creating a simple RoR application where I can create posts and also comment on them. My thought on how to do this is to have posts and comments be of the same type and utilizing the same MVC except comments have a parent id. Posts will have a parent id of 0 while comments will have the id of its parents. I was thinking of when I tried to create a comment, I could just pass in the parent's id to the new method. This did not work. Although I got no errors it seems like the @post variable in the new method is not the same @post variable in the create method. My intuition was that the new method creates a new object then passes the object to the view. The view then populates the objects parameters and then sends it to the create method and the create method saves the object to the database. From trial and error this does not seem to be the case unless I am just doing it wrong. Is there an easier way to get the same functionality I am trying to achieve? or is there a way to get my way to work? any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您观看经典的“15 分钟内构建博客”视频,该视频向您展示了如何做到这一点。
除此之外,由于帖子
has_many :comments
我不建议您执行您的建议。I would recommend you follow the classic "Build a Blog in 15 minutes" video that shows you how to do this.
Among other things, since a post
has_many :comments
I wouldn't recommend doing what you're proposing.