# RESTful app and refactoring code in different controllers

**URL:** <https://www.ruby-forum.com/t/restful-app-and-refactoring-code-in-different-controllers/161912>\
**Category:** Rails\
**Created:** [March 3, 2009, 8:32pm UTC](https://www.ruby-forum.com/t/restful-app-and-refactoring-code-in-different-controllers/161912 "2009-03-03T20:32:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 3, 2009, 8:32pm UTC](https://www.ruby-forum.com/t/restful-app-and-refactoring-code-in-different-controllers/161912/1 "2009-03-03T20:32:44Z")

</div>

My application contains following controllers:

1. Topics
2. Items
3. Attachments
4. Users

Following are the models:

1. Topics has\_many items
2. Items has\_many attachments
3. Attachments
4. Users has\_many topics
5. Cities has\_many counties
6. Counties has\_many users, topics

A topic contains many items. When I try to display a topic, I need to  
get its corresponding items, their attachments and display everything in  
a single page. In addition I have some filters on who can  
add/edit/delete topics/items. Previously I did it with ugly coding -  
with all the code pressed in the Topics controller. Now I am trying to  
make my application RESTful and stuck with many question marks.

How can I render all the topics, items, and attachments in a single  
page. Previously I used lot of partials wherein main show page would  
call \_topics partial and it would then call \_items partial etc…

Now with code being separated in different controllers I am not able to  
use partials. Is there anyway I can do it? Also how can configure my  
routes for this application. All I did was map.resources :topic,  
:has\_many =\> items, but I am not sure whether I doing it right way.

Any help appreciated.

- 

CS.

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 3, 2009, 8:38pm UTC](https://www.ruby-forum.com/t/restful-app-and-refactoring-code-in-different-controllers/161912/2 "2009-03-03T20:38:52Z")

</div>

> How can I render all the topics, items, and attachments in a single  
> page. Previously I used lot of partials wherein main show page would  
> call \_topics partial and it would then call \_items partial etc…

There shouldn’t be any problem at all doing this.

You’d have a topics page, say [yoursite.com/topics/sometopic](http://yoursite.com/topics/sometopic)

Then in the view you just loop through @topic.articles, etc. Just  
because you have a restful URL scheme, it doesn’t mean that your view  
can only display data from one model.

SH

–  
Starr H.  
My blog: [http://starrhorne.com](http://starrhorne.com)  
Check out my Helpdesk RailsKit: [http://railskits.com/helpdesk/](http://railskits.com/helpdesk/)

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 3, 2009, 9:08pm UTC](https://www.ruby-forum.com/t/restful-app-and-refactoring-code-in-different-controllers/161912/3 "2009-03-03T21:08:21Z")

</div>

Thanks Starr.

I tried that…  
But each individual item in a topic can be edited/deleted or new one  
could be added to the topic. In addition, each one has some filter on  
who can edit/delete etc…

I am unable to add these add/edit/delete links to these items. Does it  
mean something is wrong with my config/routes file?  
Apart from default route, right now I have:  
map.resources :topic, :has\_many =\> items, :collection =\>  
{:populate\_counties =\> post}  
Step by step I will add other resource associations (e.g.: map.resources  
:items :has\_many :attachments) - when I am sure whats the right way to  
do it.

Any clues?

Thanks,  
CS.

Starr H. wrote:

> > How can I render all the topics, items, and attachments in a single  
> > page. Previously I used lot of partials wherein main show page would  
> > call \_topics partial and it would then call \_items partial etc…
> 
> There shouldn’t be any problem at all doing this.
> 
> You’d have a topics page, say [yoursite.com/topics/sometopic](http://yoursite.com/topics/sometopic)
> 
> Then in the view you just loop through @topic.articles, etc. Just  
> because you have a restful URL scheme, it doesn’t mean that your view  
> can only display data from one model.
> 
> SH
> 
> –  
> Starr H.  
> My blog: [http://starrhorne.com](http://starrhorne.com)  
> Check out my Helpdesk RailsKit: [http://railskits.com/helpdesk/](http://railskits.com/helpdesk/)

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 6, 2009, 4:29am UTC](https://www.ruby-forum.com/t/restful-app-and-refactoring-code-in-different-controllers/161912/4 "2009-03-06T04:29:37Z")

</div>

I think I am doing some mistake in configuring routes.  
Here is what I have in my routes:  
map.resources :agendas do |agendas|  
agendas.resources :items

end

I am not able to access items using this. I can fetch them (list items  
related to a topic) into my view, but I am not able to form other CRUD  
links to access them.

Any help appreciated…

Thanks,  
CS.

Carlos S. wrote:

> Thanks Starr.
> 
> I tried that…  
> But each individual item in a topic can be edited/deleted or new one  
> could be added to the topic. In addition, each one has some filter on  
> who can edit/delete etc…
> 
> I am unable to add these add/edit/delete links to these items. Does it  
> mean something is wrong with my config/routes file?  
> Apart from default route, right now I have:  
> map.resources :topic, :has\_many =\> items, :collection =\>  
> {:populate\_counties =\> post}  
> Step by step I will add other resource associations (e.g.: map.resources  
> :items :has\_many :attachments) - when I am sure whats the right way to  
> do it.
> 
> Any clues?
> 
> Thanks,  
> CS.
> 
> Starr H. wrote:
> 
> > > How can I render all the topics, items, and attachments in a single  
> > > page. Previously I used lot of partials wherein main show page would  
> > > call \_topics partial and it would then call \_items partial etc…
> > 
> > There shouldn’t be any problem at all doing this.
> > 
> > You’d have a topics page, say [yoursite.com/topics/sometopic](http://yoursite.com/topics/sometopic)
> > 
> > Then in the view you just loop through @topic.articles, etc. Just  
> > because you have a restful URL scheme, it doesn’t mean that your view  
> > can only display data from one model.
> > 
> > SH
> > 
> > –  
> > Starr H.  
> > My blog: [http://starrhorne.com](http://starrhorne.com)  
> > Check out my Helpdesk RailsKit: [http://railskits.com/helpdesk/](http://railskits.com/helpdesk/)
