Forum: Ruby on Rails Calling a method in another controller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Hamza K. (Guest)
on 2006-04-05 02:04
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
Keith L. (Guest)
on 2006-04-05 02:56
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
This topic is locked and can not be replied to.