0

I am trying to build an API endpoint using active_admin and I am having trouble getting the route set up how I want them. I am also not sure if my solution for the routes is in accordance with REST best practices.
I have the following models:

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

I want to create an endpoint that will take the classification_id and the web_genre, and return all of the channels which represent the union between the two(channels that belong to the classification and have the web_genre.) I am brand new to active_admin, and newish to rest best practices so I have the following questions:

1) Which controller/action should this map to from a best practices standpoint. I was leaning toward the web_genre show action nested within classification (GET /classification/:id/web_genre/:id) but it doesn't really feel right to me.

2) Once I determine the controller/action combination how can I nest the resource in active_admin appropriately(Assuming I should be nesting the resource).

If I forgot any information please feel free to comment and I will add it. Thank you in advance for your time and assistance!

ruby_newbie
  • 3,190
  • 3
  • 18
  • 29
  • AA does exposure nice RESTful interfaces for registered resources, but I think an API service should be built separately. I'd likt to recommend having a look at more suitable libraries http://intridea.github.io/grape/ https://github.com/nesquena/rabl – baxang Dec 18 '14 at 08:20

1 Answers1

2

In general I'm not sure that ActiveAdmin is the best way to build a API. Take a look onto inherited_resources, wich is used by ActiveAdmin internally by the way.

To your questions:

  1. WebGenresController#show under /classifications/:classification_id/web_genres/:id should be used. Take respect of the plural naming in controller and path names, which will save you a lot worse.
  2. With the same belongs_to mechanism like in the Model. Wich is based inherited_resources, read there for details.
Timo Schilling
  • 3,003
  • 1
  • 17
  • 29