Calling a method in another controller


#1

Hi,

I have three controllers. I have defined two controllers to goven the
site functions. However I want to have a homepage Controller. Part of
the controller will call methods from other controllers. For example it
will get the last 5 blog posts and it will get the top 5 most discussed
articles, and display it on the homepage. I will also have some
specific homepage modules being displayed on the page.

I am trying to do something like this :

require ‘content’
class HomepageController < ApplicationController

def list
@blog_contents = BlogController.list
@article_most_discussed = ArticleController.list
etc…
end
end

But it does not seem to work. How do I reference methods in other
controllers ? Is it done ? I can replicate the code in this controller,
but that will be messy.

Thanks in advance.

Hamza


#2

Hamza Khan-Cheema wrote:

Hi,

I have three controllers. I have defined two controllers to goven the
site functions. However I want to have a homepage Controller. Part of
the controller will call methods from other controllers. For example it
will get the last 5 blog posts and it will get the top 5 most discussed
articles, and display it on the homepage. I will also have some
specific homepage modules being displayed on the page.

I am trying to do something like this :

require ‘content’
class HomepageController < ApplicationController

def list
@blog_contents = BlogController.list
@article_most_discussed = ArticleController.list
etc…
end
end

But it does not seem to work. How do I reference methods in other
controllers ? Is it done ? I can replicate the code in this controller,
but that will be messy.

Thanks in advance.

Hamza

Put common code in the application.rb controller, and then you can
access it from any of your controllers. Don’t attempt to access code in
one controller from another.

Keith