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 54
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 21
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 74
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 : How to work with ror 2.1.1 using Netbeans IDE 6.1

Ask a Rails expert : json gem error

Ask a Rails expert : Problem with break

Ask a Rails expert : activesupport string first method error

Ask a Rails expert : url_for broken ?

Ask a Rails expert : map.routes.rb pls help

Ask a Rails expert : how to h tag in controller

Ask a Rails expert : Problem with Restful routing and partial form

Ask a Rails expert : will_paginate, search and ajax

Ask a Rails expert : captcha

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