# Standard practices for RESTful remote (AJAXified) manipulati

**URL:** <https://www.ruby-forum.com/t/standard-practices-for-restful-remote-ajaxified-manipulati/106528>\
**Category:** Rails\
**Created:** [June 11, 2007, 5:52pm UTC](https://www.ruby-forum.com/t/standard-practices-for-restful-remote-ajaxified-manipulati/106528 "2007-06-11T17:52:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![madscene](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5f9b8f/32.png) [@madscene](https://www.ruby-forum.com/u/madscene)\
**Post date:** [June 11, 2007, 5:52pm UTC](https://www.ruby-forum.com/t/standard-practices-for-restful-remote-ajaxified-manipulati/106528/1 "2007-06-11T17:52:44Z")

</div>

In a nutshell:

I’ve got 2 RESTful resources and complex edit page for the parent  
resource. I want to modify the nested resource from the parent  
resource’s edit page, and I want to use AJAX to do it. I’m having a  
really hard time finding a clean way to organize the RJS and partials  
artifacts.

More detail:

I’m writing a RESTful Rails app and I’m bumping into almost exactly  
the same xhr context problem described at the very end of this post…

```
http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/59998e003c538743

```

…and in this July, 2006 blog post from Bruce W…

```
http://codefluency.com/2006/7/1/rails-views-getting-in-context

```

Question 1: Have there been any additions to core Rails that address  
this context problem?

More than anything, I feel like this context issue has introduced a  
lot of codebase uncleanliness. Uber-quick example:

I have a Foo resource with a nested Bar resource.

> From a form in foos/show.rhtml, I want to update the nested Bar  
> resource via BarsController:update.

Great…so, I hit the update action in the BarsController. In my  
respond\_to, I do a switch on the appropriate context and then render a  
context-specific RJS.

Question 2: Where should these context-specific RJS files live? Under  
views/bars or views/foos?

I think the common approach would be for (say) update\_context1.rjs to  
live under views/bars. It really bugs me, though, that this file is  
going to contain a div id for some div that is only relevant in the  
Foo’s view (a la 'page.replace\_html  
‘some\_div\_id\_in\_foos\_show\_rhtml’ …).

Alternatively, one could drop an update\_bar\_context1.rjs in views/  
foos. The div id contained therein will reference some div relevant  
to Foo, which is nice, but having the BarsController reference an RJS  
outside of its own view space likewise seems a bit dirty.

I have to think that there are others out there who are doing some  
AJAX-heavy nested resource editing. What pattern(s) have you  
followed? You know, I don’t actually own the Recipes book. Is there  
a Recipe in there for this scenario?

Thanks in advance, all.

- BD

---

<div class="post-metadata">

**Author:** ![madscene](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5f9b8f/32.png) [@madscene](https://www.ruby-forum.com/u/madscene)\
**Post date:** [June 12, 2007, 6:19am UTC](https://www.ruby-forum.com/t/standard-practices-for-restful-remote-ajaxified-manipulati/106528/2 "2007-06-12T06:19:31Z")

</div>

Brian D. wrote:

> I’m writing a RESTful Rails app and I’m bumping into almost exactly

Not that I know of.

> foos. The div id contained therein will reference some div relevant  
> to Foo, which is nice, but having the BarsController reference an RJS  
> outside of its own view space likewise seems a bit dirty.
> 
> I have to think that there are others out there who are doing some  
> AJAX-heavy nested resource editing. What pattern(s) have you  
> followed? You know, I don’t actually own the Recipes book. Is there  
> a Recipe in there for this scenario?

I’ve run into some of the same problems. I’m beginning to to think the  
answer may be to move from RJS to a more client-side JS interface. Why  
should the controller/action need to know everywhere it is called from  
and how to update those pages. It would make more sense to keep the  
RESTful controller simple and put the view logic in the view. Take a  
look at Jester  
([Jester 1.3: Jsonic REST](http://giantrobots.thoughtbot.com/2007/6/11/jester-1-3-jsonic-rest)). It  
is implementing ActiveResource in JS. You can retrieve the data object  
instead of just instructions on how to rewrite the page. Then the page  
that requested the CRUD operation can alter itself as it sees fit  
without the controller needing any knowledge of where it was called  
from.

I’ve not gone very far down this road so I don’t know all the potholes  
that await, but the dabbling I’ve done has gone well so far.

Jack

---

<div class="post-metadata">

**Author:** ![madscene](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5f9b8f/32.png) [@madscene](https://www.ruby-forum.com/u/madscene)\
**Post date:** [June 12, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/standard-practices-for-restful-remote-ajaxified-manipulati/106528/3 "2007-06-12T16:28:38Z")

</div>

Thanks for the response, Jack. I’ll take a look at Jester.

Has anyone else out there seen/built a RESTful app with this type of  
behavior (modifying a nested resource via AJAX from a parent resource  
page)? I’d love to see an example of its structure.

- BD
