You are here: Forums Ask a Rails expert How to Call Controller from an...
Posted in Forums : Ask a Rails expert
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, |
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). |
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. |
Authority 37
Posting Rating 54
Sign in to rate this post
|
Hi harish , 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:
hope this will be helpful for you… bye |
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 |
Authority 50
Posting Rating 19
Sign in to rate this post
|
Don’t use Components. Components are deprecated: |
Authority 12
Posting Rating 96
Sign in to rate this post
|
But my assumption was only the use of COMPONENTS folder was deprecated |
Authority 25
Posting Rating 0
Sign in to rate this post
|
Harish, but i also prefer “render_component” method instead of this….. |
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 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 |
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…...... |
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. |
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