# AJAX - REST & nested resources

**URL:** <https://www.ruby-forum.com/t/ajax-rest-nested-resources/152614>\
**Category:** Rails\
**Created:** [November 5, 2008, 5:08pm UTC](https://www.ruby-forum.com/t/ajax-rest-nested-resources/152614 "2008-11-05T17:08:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 5, 2008, 5:08pm UTC](https://www.ruby-forum.com/t/ajax-rest-nested-resources/152614/1 "2008-11-05T17:08:24Z")

</div>

I’ve created a set of resources, with scaffolds that all work. I then  
changed one resource to be nested inside another as a one to many  
relation. This required a little bit of work on the tests, the  
controllers and the views but it all works. Now I want to go the extra  
step and have the child resources become updateable inside the parent  
form. I’ve pushed the formatting of the child forms into partials which  
I can call inside the parent form. This allows me to display a list of  
the children inside a parent form. Standard stuff. Now the next step …

I want to use AJAX to update the child models inside the parent form.  
The idea is that when I’ve got a list of the children on the parent  
form, I can click the edit/delete links for a line and it will pull up  
the edit form inside the parent form, allow me to edit the record, then  
update it and close the section down again. I’ve got most of this done,  
I can get as far as AJAXing an edit form for the child into the parent  
form, updating that, which updates the activeRecord and then … Well  
it still sends me off to the show method on the child controller, not  
what I want so I have to do the next step and …

Someone else must have done this, I don’t want to have to re-invent the  
wheel at every step, it’s such a common task there must be some good  
step by step examples. But I can’t find them.

I’ve tried using ActiveScaffold which is OK, but it doesn’t really do  
what I need it to do, and it’s broken in Rails 2.1, which requires  
patching some fixes into the current code and the fixes are things that  
were done ages and ages ago but aren’t in the current release and … ,  
I’ve just not got the patience for this sort of thing. Which is why I  
thought I’d do the job simply and cleanly by working forwards slowly  
from the autogenerated scaffold code. So please don’t suggest  
ActiveScaffold.

Any other suggestions?

If there really are no good examples for such a common design pattern  
I’ll put up a solution somewhere public when I’ve done it. But I can’t  
believe there isn’t one already.

Thanks

John S.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 6, 2008, 7:47am UTC](https://www.ruby-forum.com/t/ajax-rest-nested-resources/152614/2 "2008-11-06T07:47:59Z")

</div>

AndyV wrote:

> There’s really no magic here. The default response to an update in  
> the scaffolded controller is to redirect to the show event. If you  
> want to do something different (ie., the convention doesn’t work for  
> you) then go to the scaffolded controller and change it. I’d guess  
> you want to redirect to the show event of the parent (rather than the  
> child).
> 
> On Nov 5, 11:08 am, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Yes, it’s not magical. It’s just a case of working through the changes,  
which I went ahead and did. I’d made it just that little bit more  
difficult for myself by trying to keep the old non-ajaxified actions  
running in parallel, which as you say redirect to the show in the child  
not the parent.

Mmmm since it’s so general I wonder if it’s possible to write a  
generator to implement the pattern, saves all that typing.

Thanks

John S.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 5, 2008, 9:52pm UTC](https://www.ruby-forum.com/t/ajax-rest-nested-resources/152614/3 "2008-11-05T21:52:35Z")

</div>

There’s really no magic here. The default response to an update in  
the scaffolded controller is to redirect to the show event. If you  
want to do something different (ie., the convention doesn’t work for  
you) then go to the scaffolded controller and change it. I’d guess  
you want to redirect to the show event of the parent (rather than the  
child).

On Nov 5, 11:08 am, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
