Tag: nested routes

嵌套路由和CRUD操作以及Rails中的附加值

我在rails api中通过关系创建了一个has_many。 我也使用嵌套路线。 我的模特如下; class Author :comments end class Post :comments end class Comment < ActiveRecord::Base belongs_to :author belongs_to :post end 我的路线如下; Rails.application.routes.draw do namespace :api, defaults: { :format => ‘json’ } do resources :posts do resources :comments end resources :authors end end 所以我的目标是 注释是嵌套路由,以便我可以创建和显示post的注释 这里没有任何post的作者。 作者是评论所有者 我实现了概念并且几乎都在工作。 但我面临以下两个协会问题 如何在父创建时为关联表添加其他字段。 这里我的要求是在创建post时,我需要插入一个默认条目进行评论。 我的post控制器实现如下所示 def create params […]