16

I have a resource called User and another one called Order.

I want Order to be nested inside Users so I can have these routes:

/users
/users/:id
/users/:id/new
/users/:id/edit
/users/:user_id/orders
/users/:user_id/orders/:id
/users/:user_id/orders/:id/new
/users/:user_id/orders/:id/edit

How can I do that with activeadmin?

Bishma Stornelli
  • 2,539
  • 4
  • 22
  • 30

2 Answers2

21

Just add belongs_to option to active_admin resource page

ActiveAdmin.register Order do
  belongs_to :user
end
railscard
  • 1,848
  • 16
  • 12
12

@railscard's answer is partially correct, but if you don't want the default routes like /order, /order/:id etc like mentioned by @bishma-stornelli - you could add the option like this :

ActiveAdmin.register Order do
  belongs_to :user, :optional => true
end
rohitpaulk
  • 407
  • 6
  • 11
  • Looks like they've changed the defaults, because now when I've added `optional: true`, i got access to both `/users/:user_id/orders` and `/orders` (which I was looking for) – Andrew Rozhenko Jan 15 '20 at 13:03
  • If you still want your belongs_to resources to be available in the default menu and through non-nested routes, you can use the :optional option. [active_admin docs](https://activeadmin.info/2-resource-customization.html#belongs-to) – Andrew Rozhenko Jan 15 '20 at 13:46