【问题标题】:Active admin nested_route for API endpointAPI 端点的活动管理员 nested_route
【发布时间】:2014-12-15 21:24:33
【问题描述】:

我正在尝试使用 active_admin 构建 API 端点,但无法按照我想要的方式设置路由。我也不确定我的路由解决方案是否符合 REST 最佳实践。
我有以下型号:

class Classification < ActiveRecord::Base

end

class Channel < ActiveRecord::Base
belongs_to :classification
has_many :channel_genres
has_many :channel_web_genres
has_many :web_genres, through: :channel_web_genres
end

class ChannelWebGenre < ActiveRecord::Base
belongs_to: channel
belongs_to: web_genre
end 

class WebGenre < ActiveRecord::Base

end

我想创建一个端点,它将采用分类 ID 和 web_genre,并返回代表两者之间联合的所有频道(属于分类并具有 web_genre 的频道。)我是全新的 active_admin ,以及新的休息最佳实践,所以我有以下问题:

1) 从最佳实践的角度来看,这应该映射到哪个控制器/操作。我倾向于嵌套在分类中的 web_genre 显示操作 (GET /classification/:id/web_genre/:id),但它对我来说真的不合适。

2) 一旦我确定了控制器/动作组合,我如何才能适当地将资源嵌套在 active_admin 中(假设我应该嵌套资源)。

如果我忘记了任何信息,请随时发表评论,我会添加它。提前感谢您的时间和帮助!

【问题讨论】:

标签: ruby-on-rails rest activeadmin nested-routes


【解决方案1】:

总的来说,我不确定 ActiveAdmin 是构建 API 的最佳方式。看看inherited_resources,顺便提一下,ActiveAdmin 在内部使用它。

对于您的问题:

  1. 应该使用 /classifications/:classification_id/web_genres/:id 下的 WebGenresController#show。尊重控制器和路径名称中的复数命名,这会为您节省很多。
  2. 使用与模型中相同的belongs_to 机制。它是基于inherited_resources的,详情请阅读there

【讨论】:

  • 来自 AA 开发者的回答 :)
猜你喜欢
  • 2017-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-27
  • 2012-03-23
  • 2018-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多