Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert How to Call Controller from an...

Replytotopic

How to Call Controller from another

Posted in Forums : Ask a Rails expert

 
Profile

Authority 87
Posting Rating 16
Sign in to rate this post

Hi All,

I want to call controller from another controller and get the controller methods .is it possible?

If it is possible, plz help me

Thanks in Advance,
Harish

 
Me

Authority 62
Posting Rating 100
Sign in to rate this post

I’m sure that it’s somehow possible, however, you just don’t do that. Each request maps to one controller and only one. If you need interaction between controllers, you either redirect at the end of a request or put data in the session or the flash (or both).

 
2660287362

Authority 50
Posting Rating 19
Sign in to rate this post

It sounds to me like you want to reuse existing code that’s in one controller in a different one. What you’ll probably want to do is pull that common code out into a module, put it in /lib, then include that module in both controllers.

 
Atgaaaam92y6g5nj0ahce71euqlog7apdmnrdwyruc0a6gdceosrtpybscluzngbydxils0r2utsaqfvb6ofljahnwepajtu9vduspwimkrbmlzngha9qwp0fdw4yq

Authority 37
Posting Rating 56
Sign in to rate this post

Hi harish ,
What i understood is you have more than 1 controller in your app. say controller A , Controller B & so on and now you want to use the ” action ” of “controller A” in” controller B ” or vice versa…...
if this is the case then….

solution 1: If you have actions which is common to many controllers then you can write this action in your application controller ..and that is accessible to all controllers as local action.

solution2:
ex: you have “show” action in “controller A” and now you want to access in “controller B” then

  • redirect_ to :controller => :A , :action => :show, :id=>@myobj

    or

  • render :controller => :A , :action => :show

hope this will be helpful for you…
bye
 
Profile

Authority 12
Posting Rating 96
Sign in to rate this post

I think render_component will serve your purpose

http://rails.rubyonrails.com/classes/ActionController/Components.html

http://wiki.rubyonrails.org/rails/pages/Components

 
2660287362

Authority 50
Posting Rating 19
Sign in to rate this post

Don’t use Components.

Components are deprecated:
http://www.rubyonrails.org/deprecation

 
Profile

Authority 12
Posting Rating 96
Sign in to rate this post

But my assumption was only the use of COMPONENTS folder was deprecated

http://6brand.com/rails-components-abandoned

 
Th

Authority 25
Posting Rating 0
Sign in to rate this post

Harish,
if u really want to call a method in another controller create an instance of that controller class and call the perticular method of it.
”@applicationContoller_obj = ApplicationController.new
@applicationContoller_obj.method()”

but i also prefer “render_component” method instead of this…..

 
Yi

Authority 62
Posting Rating 22
Sign in to rate this post

Let’s say you have AController and BController. And in BController, you want to call AControllerA.method_a

Do you notice the the first line of the Controller file, which looks like:

“class AController < ApplicationController”

This line means both AController and BController are subclass of ApplicationController.

The inheritance relationship of this 3 class is:

ApplicationController
- AController
- BController

Thus to share a method between AController and BController, you can put the method in ApplicationController.

Or if you have a bunch of methods need to shared in both class, you can create another super class of both:

ApplicationController
+- CommonController +- AController +- BController

 
Th

Authority 25
Posting Rating 0
Sign in to rate this post

Sorry i did a little mistake here in my last post…Ya its no need to initiate an object of a ActionController from another controller because all controllers already inherited from ActionController…......

 
Profile

Authority 37
Posting Rating 76
Sign in to rate this post

You would hardly need such call. If you need to, pull it out in a separate module as specified by Jason or put into a private method in your application controller. I would not recommend calling an instance of a controller in another controller.

Replytotopic

Other Recent Topics

Ask a Rails expert : Couldn't find Product without an ID

Ask a Rails expert : HTML to XML ---> get title & description

Ask a Rails expert : Accessing controller actions from scripted page?

Ask a Rails expert : seledted option for select_tag

Ask a Rails expert : Merging fields from two tables into one, including duplicates

Ask a Rails expert : saving has_many :through

Ask a Rails expert : Use Rails to develop sites for both Designer and Programmer

Ask a Rails expert : Rails+RS232

Ask a Rails expert : Is this a good way to add Admin section

Ask a Rails expert : RSS feed maker in rails 2.1

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel