【发布时间】:2017-11-26 13:49:27
【问题描述】:
我正在学习在 Rails 中使用嵌套资源。为了保持简单和可重复,我设置了一个非常简单的博客应用程序,其中包含两个资源 posts 和 comments。大部分代码都是脚手架生成的。
我已成功修改post#show 以在每个帖子下方显示comments,指向Add New Comment 的链接也呈现new comment 表单。但是我无法保存 cmets 并出现路由错误:
ActionController::RoutingError (没有路由匹配 [POST] "/posts/1/cmets/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 的答案对我不起作用。
【问题讨论】:
标签: ruby-on-rails rails-routing nested-resources