首页 > 解决方案 > 使用 has_and_belongs_to_many 时,更新时的浅嵌套不知道父级

问题描述

从文档中,以下用于编辑和更新的路线将类似于 /comments/:id

resources :articles do
  resources :comments, shallow: true
end

当我更新评论并且它不知道文章时,您如何返回评论索引?在这种情况下你会回到文章索引吗?

在使用浅嵌套时更新评论后是否有可接受的解决方法来返回评论列表?

更新我的模型使用 has_and_belongs_to_many 关联,因此我无法通过 @comment.article 获取父级。

我的模型

class Venue < ApplicationRecord
  has_and_belongs_to_many :users
end

class User < ApplicationRecord
  has_and_belongs_to_many :venues
end

我的路线

resources :venues do
      resources :accounts, shallow: true
end

标签: ruby-on-railsruby-on-rails-5nested-routes

解决方案


推荐阅读