Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在rails中使用嵌套资源时如何修改表单__Ruby On Rails_Rails Routing_Nested Resources - Fatal编程技术网

Ruby on rails 在rails中使用嵌套资源时如何修改表单_

Ruby on rails 在rails中使用嵌套资源时如何修改表单_,ruby-on-rails,rails-routing,nested-resources,Ruby On Rails,Rails Routing,Nested Resources,我正在学习使用rails中的嵌套资源。为了保持它的简单性和可复制性,我设置了一个非常简单的博客应用程序,其中包含两个资源posts和comments。大多数代码都是由scaffold生成的 我已成功修改了post#show以在每篇帖子下方显示评论,指向添加新评论的链接也会呈现新评论表单。但是,我无法保存注释并获得路由错误: ActionController::RoutingError(没有与[POST]匹配的路由“/posts/1/comments/new” 注释部分形式的比较 原始/嵌套前资源

我正在学习使用rails中的嵌套资源。为了保持它的简单性和可复制性,我设置了一个非常简单的博客应用程序,其中包含两个资源
posts
comments
。大多数代码都是由scaffold生成的

我已成功修改了
post#show
以在每篇帖子下方显示
评论
,指向
添加新评论
的链接也会呈现
新评论
表单。但是,我无法保存注释并获得路由错误:

ActionController::RoutingError(没有与[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| %>

已修改/嵌套资源后:

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

我已经将代码推到,使用独立的分支
master
,没有嵌套资源,使用嵌套资源
nestedRoutes
。非常感谢社区的帮助


注意:对的回答对我不起作用。

当您可能希望表单转到创建路径时,您传递的url看起来像是
新的\u帖子\u评论\u路径。
。根据您的路径,我假设这看起来像
帖子\u评论\u路径

这就是为什么您会看到错误,没有为
new
路径定义POST方法(仅get)