gpt4 book ai didi

ruby-on-rails - 在rails中使用嵌套资源时如何修改form_with

转载 作者:行者123 更新时间:2023-12-02 01:06:01 25 4
gpt4 key购买 nike

我正在学习在 Rails 中使用嵌套资源。为了保持简单和可重现性,我设置了一个非常简单的博客应用程序,其中包含两个资源 postscomments。大部分代码都是由脚手架生成的。

我已成功修改 post#show 以在每个帖子下方显示 commentsAdd New Comment 的链接也呈现 新的评论 形式。但是我无法保存评论并收到路由错误:

ActionController::RoutingError (No route matches [POST] "/posts/1/comments/new"

评论部分形式比较

原始/嵌套资源之前:

<%= form_with(model: comment, local: true) do |form| %>

修改/嵌套资源后:

<%= form_with(url: new_post_comment_path, 
scope: :comment, local: true) do |form| %>

我已将代码推送到 github repo ,具有不带嵌套资源的单独分支 master 和带嵌套资源的 nestedRoutes。将感谢社区的帮助。

注意:Nested Resources w/ Rails 5.1 form_with 的答案对我不起作用。

最佳答案

看起来您传递的 url 是 new_post_comment_path,当您可能希望您的表单进入创建路径时。根据您的路线,我假设这看起来像 post_comments_path

这就是您看到错误的原因,没有为 new 路径定义 POST 方法(仅 GET)。

关于ruby-on-rails - 在rails中使用嵌套资源时如何修改form_with,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47496836/

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