【问题标题】:how to modify form_with when using nested resources in rails在rails中使用嵌套资源时如何修改form_with
【发布时间】:2017-11-26 13:49:27
【问题描述】:

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

我已成功修改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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-30
      • 2010-11-29
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多