2

A friend of mine sent me some code who was having trouble understanding how nested resources work in Rails. Having worked with Rails for many years, I quickly explained it to him and then shared some code. Then I enhanced the code to show him an even better way.

You can see the code at this gist. His code first, followed by my quick explanation code, and then a more complete/robust version.

There are many ways to approach this. My question is, is there a better / more robust / more solid / more flexible / etc way than this? How do you other professionals do it?

Brad Werth
  • 17,411
  • 10
  • 63
  • 88
Kevin Elliott
  • 2,630
  • 3
  • 23
  • 21
  • 1
    how come you're so unsure of your params? – apneadiving Aug 29 '12 at 07:03
  • Two ways to get to photos. /admin/photos and /admin/museum/:id/photos – Kevin Elliott Aug 29 '12 at 07:04
  • 1
    so the same view is meant to update either a museum or a photo? – apneadiving Aug 29 '12 at 07:08
  • No, the same view is meant to CRUD on a photo, whether it is via a museum->photo(s) click path, or via the photos directly. In this scenario, the admin interface allows the admin to manage all photos (/admin/photos) irregardless of their association with a museum, or for a particular museum (/admin/museums/:id/photos) via it's nested route/scope. And it does this in a DRY manner. – Kevin Elliott Aug 29 '12 at 07:10
  • 1
    so why do you call the form with in two different ways? – apneadiving Aug 29 '12 at 07:11
  • Technically, it's using a form partial, so it's only using different form tags to maintain the proper POST path, in this particular case (editing a photo). Only the
    wrapper itself is repeated.
    – Kevin Elliott Aug 29 '12 at 07:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/15936/discussion-between-apneadiving-and-kevin-elliott) – apneadiving Aug 29 '12 at 07:13

1 Answers1

2

Ended up with a solution introducing presenters.

Code is in the gist created by OP

apneadiving
  • 114,565
  • 26
  • 219
  • 213
  • Great approach with presenters. I've used them before, but did not want to introduce them to my friend with the green thumb (he's still new to a lot of this). – Kevin Elliott Aug 29 '12 at 08:25
  • apneadiving's approach to the controller action was pretty interesting. It's slimmer, does much of what the original does, and is more clear. It does need some additional conditionals if you want to redirect on error, etc, but really it seems like a good approach. – Kevin Elliott Aug 29 '12 at 08:26
  • I'm a Rails noob, but we settled on something similar. Of course, we put our strings into en.yml, but your code is factored well enough that you shouldn't have any problem adding that on later. – Jason Oct 16 '12 at 19:02