Display a page using 2 controllers

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 (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.

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. [email protected]

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 (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.

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.!!