# 2 forms in one page,how to arrange the code and do it restful?

**URL:** <https://www.ruby-forum.com/t/2-forms-in-one-page-how-to-arrange-the-code-and-do-it-restful/154941>\
**Category:** Rails\
**Created:** [December 5, 2008, 11:28am UTC](https://www.ruby-forum.com/t/2-forms-in-one-page-how-to-arrange-the-code-and-do-it-restful/154941 "2008-12-05T11:28:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSSBMpKSSzE5SSB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/57b2e6/32.png) [@SSSBMpKSSzE5SSB](https://www.ruby-forum.com/u/SSSBMpKSSzE5SSB)\
**Post date:** [December 5, 2008, 11:28am UTC](https://www.ruby-forum.com/t/2-forms-in-one-page-how-to-arrange-the-code-and-do-it-restful/154941/1 "2008-12-05T11:28:23Z")

</div>

Backgroup:  
Customer has many old documents(just some scanning pictures) and want  
to store them into computer,we need to input the doc and relate them  
to the existing authors in DB.

Key works: document;author

UI of document input page:  
left side is the document input form  
right side is the author search form

Problem:  
How should i arrange the code to deal with it.  
I have 2 solutions, but i do not like them all! I think maybe i have  
some misunderstanding of restful!  
Solution1:  
let the document handles them all, put the current page under folder  
“views/documents”, make the author search form a partial template and  
locate it under folder “views/documents” too, put the search\_author  
method in DocumentsController.this really works,but it break the REST  
rules, how could i write the url for search\_author method?  
I think the search\_author method should be write in  
AuthorsController.If I do this, i can not get the search result  
@authors from AuthorsController.

Solution2:  
It seem that the document belongs to the author. so i use the nested  
resources and add this line in route.rb  
map.resources :authors,:has\_many=\>:documents  
but this require that every operation on document should under a given  
author, the “action” attribute of document form should be like this  
“authors/2/documents”, but I do not know the author until the client  
side user select one from the search result, it happens at client  
side.

I really confused about this and hope for your suggestion!

---

<div class="post-metadata">

**Author:** ![SSSBMpKSSzE5SSB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/57b2e6/32.png) [@SSSBMpKSSzE5SSB](https://www.ruby-forum.com/u/SSSBMpKSSzE5SSB)\
**Post date:** [December 5, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/2-forms-in-one-page-how-to-arrange-the-code-and-do-it-restful/154941/2 "2008-12-05T17:15:48Z")

</div>

AFAIK the RESTful part is unrelated to the view part.  
REST should deal with resources only, so:  
/documents should return an index of all docs,  
/authors should return an index of all of them,  
then an association should be created when POSTing to /authors/  
id\_from\_right\_pane/documents/id\_from\_left\_pane.  
It should be usable without defining any view.  
Afterwards, on top of that, build the view.

---

<div class="post-metadata">

**Author:** ![SSSBMpKSSzE5SSB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/57b2e6/32.png) [@SSSBMpKSSzE5SSB](https://www.ruby-forum.com/u/SSSBMpKSSzE5SSB)\
**Post date:** [December 6, 2008, 11:43am UTC](https://www.ruby-forum.com/t/2-forms-in-one-page-how-to-arrange-the-code-and-do-it-restful/154941/3 "2008-12-06T11:43:16Z")

</div>

As you know, the document create page contains the search\_author Form  
in the right side. And the question is where should i locate the  
“search\_author” method?  
If at DocumentsController, how could i write the url for  
“search\_author” restful?  
If at AuthorsController, how could i connect the AuthorsController  
with the view of DocumentsController?

On 12ÔÂ6ÈÕ, ÉÏÎç12Ê±14·Ö, Klaus P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![SSSBMpKSSzE5SSB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/57b2e6/32.png) [@SSSBMpKSSzE5SSB](https://www.ruby-forum.com/u/SSSBMpKSSzE5SSB)\
**Post date:** [December 8, 2008, 8:37pm UTC](https://www.ruby-forum.com/t/2-forms-in-one-page-how-to-arrange-the-code-and-do-it-restful/154941/4 "2008-12-08T20:37:54Z")

</div>

I would give a try at this:  
For resources in routes.rb:  
map.resources :authors  
map.resources :documents  
map.resources :authors, :has\_many =\> :documents

So, documents should be accessible by themselves or as sub-resources  
of authors.

For controllers, both authors and documents should return an index of  
all of them because you want to pair them up.  
Or, try using :collection in routes.rb to GET only :unassigned  
documents (thinking aloud here).

Finally, for your Documents view, use Ajax to make a RESTful call to  
all available Authors and populate a listbox.  
And when you Submit the form with paired-up Authors and Documents,  
make it an Ajax POST to authors/auth\_id/documents with param doc\_id.  
You’ll have to manage also updates and deletes.

This was just a plan, I’d like to know if it can work this way.

Cheers!
