# Display a page using 2 controllers

**URL:** <https://www.ruby-forum.com/t/display-a-page-using-2-controllers/138769>\
**Category:** Rails\
**Created:** [May 27, 2008, 4:42pm UTC](https://www.ruby-forum.com/t/display-a-page-using-2-controllers/138769 "2008-05-27T16:42:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [May 27, 2008, 4:42pm UTC](https://www.ruby-forum.com/t/display-a-page-using-2-controllers/138769/1 "2008-05-27T16:42:42Z")

</div>

Hi everybody,

I’m currently trying to write my own blog on Rails 2. In this way, I had  
create 2 models and 3 tables:

- articles
- tags
- articles\_tags

After this, I had generated a scaffold of Article in order to use the  
blog with an address such as: [http://domain.tld/articles/new](http://domain.tld/articles/new) (for  
example)

Now I would like to display a list of tag on every pages on the left  
too. But my problem is that:  
to do so, I need to use a other controller then articles, even if the  
url need to require articles controller.

In my opinion, it’s possible by using this king of line into the  
application.html.erb view:  
\<%= render\_component :controller =\> “tags”, :action =\> “listing” %\>

and this file in views/listing.html.erb:

 \<% for tag in @tags %\> - \<%=h tag.name %\>
 \<% end %\> 

But after this, I really do not understand why I got a infinit loop.  
Here you can see my log:

Processing ArticlesController#new (for 127.0.0.1 at 2008-05-27 16:49:30)  
[GET]  
Session ID:  
BAh7CDoMY3NyZl9pZCIlMTU2MDg2ZmUzZGIzOGRlM2VjZDViMTZhMWI2MDA0%0AZmIiCmFkbWluVCIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6%0AOkZsYXNoSGFzaHsABjoKQHVzZWR7AA%3D%3D–f08000449ccdeecc8ecd1e5c67f49baad76ebbcc  
Parameters: {“action”=\>“new”, “controller”=\>“articles”}  
WARNING: You’re using the Ruby-based MySQL library that ships with  
Rails. This library is not suited for production. Please install the  
C-based MySQL library instead (gem install mysql).  
SQL (0.000221) SET NAMES ‘utf8’  
SQL (0.000180) SET SQL\_AUTO\_IS\_NULL=0  
Article Columns (0.421418) SHOW FIELDS FROM `articles`  
Rendering template within layouts/application  
Rendering articles/new  
User Load (0.001248) SELECT \* FROM `users`  
User Columns (0.002785) SHOW FIELDS FROM `users`  
Category Load (0.000625) SELECT \* FROM `categories`  
Category Columns (0.002134) SHOW FIELDS FROM `categories`  
Start rendering component ({:controller=\>“tags”, :action=\>“listing”}):

Processing TagsController#listing (for 127.0.0.1 at 2008-05-27 16:49:31)  
[GET]  
Session ID:  
BAh7CDoMY3NyZl9pZCIlMTU2MDg2ZmUzZGIzOGRlM2VjZDViMTZhMWI2MDA0%0AZmIiCmFkbWluVCIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6%0AOkZsYXNoSGFzaHsABjoKQHVzZWR7AA%3D%3D–f08000449ccdeecc8ecd1e5c67f49baad76ebbcc  
Parameters: {“action”=\>“listing”, “id”=\>nil, “controller”=\>“tags”}  
Tag Load (0.059799) SELECT \* FROM `tags`  
Rendering template within layouts/application  
Rendering tags/listing  
Tag Columns (0.002136) SHOW FIELDS FROM `tags`  
Start rendering component ({:controller=\>“tags”, :action=\>“listing”}):

Processing TagsController#listing (for 127.0.0.1 at 2008-05-27 16:49:31)  
[GET]  
Session ID:  
BAh7CDoMY3NyZl9pZCIlMTU2MDg2ZmUzZGIzOGRlM2VjZDViMTZhMWI2MDA0%0AZmIiCmFkbWluVCIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6%0AOkZsYXNoSGFzaHsABjoKQHVzZWR7AA%3D%3D–f08000449ccdeecc8ecd1e5c67f49baad76ebbcc  
Parameters: {“action”=\>“listing”, “id”=\>nil, “controller”=\>“tags”}  
CACHE (0.000000) SELECT \* FROM `tags`  
Rendering template within layouts/application  
Rendering tags/listing  
Start rendering component ({:controller=\>“tags”, :action=\>“listing”}):

Processing TagsController#listing (for 127.0.0.1 at 2008-05-27 16:49:31)  
[GET]  
Session ID:  
BAh7CDoMY3NyZl9pZCIlMTU2MDg2ZmUzZGIzOGRlM2VjZDViMTZhMWI2MDA0%0AZmIiCmFkbWluVCIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6%0AOkZsYXNoSGFzaHsABjoKQHVzZWR7AA%3D%3D–f08000449ccdeecc8ecd1e5c67f49baad76ebbcc  
Parameters: {“action”=\>“listing”, “id”=\>nil, “controller”=\>“tags”}  
CACHE (0.000000) SELECT \* FROM `tags`  
Rendering template within layouts/application  
Rendering tags/listing  
Start rendering component ({:controller=\>“tags”, :action=\>“listing”}):

Processing TagsController#listing (for 127.0.0.1 at 2008-05-27 16:49:31)  
[GET]  
Session ID:  
BAh7CDoMY3NyZl9pZCIlMTU2MDg2ZmUzZGIzOGRlM2VjZDViMTZhMWI2MDA0%0AZmIiCmFkbWluVCIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6%0AOkZsYXNoSGFzaHsABjoKQHVzZWR7AA%3D%3D–f08000449ccdeecc8ecd1e5c67f49baad76ebbcc  
Parameters: {“action”=\>“listing”, “id”=\>nil, “controller”=\>“tags”}  
CACHE (0.000000) SELECT \* FROM `tags`  
Rendering template within layouts/application  
Rendering tags/listing  
Start rendering component ({:controller=\>“tags”, :action=\>“listing”}):

…and if I don’t stop the server, it continue to loop infinitly like  
that :s

Thank you for any help.

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [May 28, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/display-a-page-using-2-controllers/138769/2 "2008-05-28T18:00:52Z")

</div>

Your “Tag” controller is using the application.html.erb layout as  
well, so it continuously tries to render itself!

You need to create another layout file for the Tag controller. This  
way, when application.html.erb is rendered, it will display the Tag  
controller without entering the infinite loop.

Regards,

Rey9999

On 27 Mag, 16:42, Zangief I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [May 28, 2008, 8:02pm UTC](https://www.ruby-forum.com/t/display-a-page-using-2-controllers/138769/3 "2008-05-28T20:02:07Z")

</div>

Zangief I. wrote:

> Hi everybody,
> 
> I’m currently trying to write my own blog on Rails 2. In this way, I had  
> create 2 models and 3 tables:
> 
> - articles
> - tags
> - articles\_tags
> 
> After this, I had generated a scaffold of Article in order to use the  
> blog with an address such as: [http://domain.tld/articles/new](http://domain.tld/articles/new) (for  
> example)
> 
> Now I would like to display a list of tag on every pages on the left  
> too. But my problem is that:  
> to do so, I need to use a other controller then articles, even if the  
> url need to require articles controller.
> 
> In my opinion, it’s possible by using this king of line into the  
> application.html.erb view:  
> \<%= render\_component :controller =\> “tags”, :action =\> “listing” %\>
> 
> Thank you for any help.

You may just be better off with a helper and a partial

class ApplicationHelper  
def tag\_cloud  
tags = Tag.find(:all)  
render :partial =\> ‘tags/listing’, :locals =\> { :tags =\> tags }  
end  
end

And move that erb snippet to views/tags/listing.html.erb. Now in your  
layout you can just do:

\<%= tag\_cloud %\>

You should know that components are rarely used. Mainly because Rails  
treats it as a whole new request, with all the performance overhead that  
comes with that. And most often, they can be replaced by a simple  
healer instead.

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [May 28, 2008, 9:56pm UTC](https://www.ruby-forum.com/t/display-a-page-using-2-controllers/138769/4 "2008-05-28T21:56:33Z")

</div>

Alex W. wrote:

> You may just be better off with a helper and a partial
> 
> class ApplicationHelper  
> def tag\_cloud  
> tags = Tag.find(:all)  
> render :partial =\> ‘tags/listing’, :locals =\> { :tags =\> tags }  
> end  
> end
> 
> And move that erb snippet to views/tags/listing.html.erb. Now in your  
> layout you can just do:
> 
> \<%= tag\_cloud %\>

Wouw! Now it’s work perfectly. Thank you very much Alex W.!!
