# Action Placed in Application Controller Doesn't Inherit Down

**URL:** <https://www.ruby-forum.com/t/action-placed-in-application-controller-doesnt-inherit-down/172726>\
**Category:** Rails\
**Created:** [July 27, 2009, 3:39am UTC](https://www.ruby-forum.com/t/action-placed-in-application-controller-doesnt-inherit-down/172726 "2009-07-27T03:39:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [July 27, 2009, 3:39am UTC](https://www.ruby-forum.com/t/action-placed-in-application-controller-doesnt-inherit-down/172726/1 "2009-07-27T03:39:50Z")

</div>

I reasoned that placing an action in the application controller would  
be roughly equivalent to placing that action in each of the other  
controllers since each of the other controllers inherits from the  
application controller. It turns out that that doesn’t work. It  
behaves as if the action is missing from the other controllers. I  
thinking that, for whatever reason, when Rails looks for an action it  
only looks at the controller and doesn’t look up the chain. If the  
action isn’t there, it behaves accordingly. Can anyone tell me why  
that is and what I should do about it if I want the action in the  
application controller to be available in several other controllers?

Thanks for any input.

```
    ... doug
```

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [July 27, 2009, 4:05am UTC](https://www.ruby-forum.com/t/action-placed-in-application-controller-doesnt-inherit-down/172726/2 "2009-07-27T04:05:01Z")

</div>

Since all controllers are inherited from ApplicationController,  
methods of ApplicetionController are only inherited as “methods” and  
not as “actions”. You can use these inherited methods like normal  
methods, but you can not use them as “actions” (i.e., methods that are  
executed upon a request via routing).

Requiring the same action for multiple controllers is quite uncommon,  
there may be better methods to do the same thing.  
But still you can use this -

class ApplicationController \< ActionController::Base  
def common\_action  
#code  
end  
end

class ExampleController \< ApplicationController

def common\_action  
super  
#render ‘shared/common\_action’ #uncomment this line if you want to  
use a common template too.  
end  
end

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [July 27, 2009, 9:11pm UTC](https://www.ruby-forum.com/t/action-placed-in-application-controller-doesnt-inherit-down/172726/3 "2009-07-27T21:11:13Z")

</div>

First of all, your solution works great! Thanks.

> Requiring the same action for multiple controllers is quite uncommon,  
> there may be better methods to do the same thing.

Believe me, when someone tells me something like that, I listen  
because the person speaking is usually right. I really want to avoid  
doing things in an unorthodox way and/or in a way that makes things  
more difficult than they need to be. Perhaps the problem is that I  
have too many controllers. In this case, I have a ‘main’ controller  
for general purpose actions and an ‘admin’ controller for  
administrative actions. Access to the admin controller is limited to  
the admin user. The action in question provides a gateway to all of  
the CRUD capabilities; however, I want only the administrative user to  
have access to the CUD capabilities. In addition, the views are also  
different because the admin view includes the CUD capabilities. Given  
these circumstances, I thought it would be nice to have a copy of this  
action in both the admin controller and the main controller. But, I  
want to keep my code DRY. So, it seemed to me that the logical thing  
to do would be to place the action in the application controller and  
let it inherit down to both the main and admin controllers. It seems  
to work just fine. Do you still think that I’m on the wrong track?

Thanks for the input.

```
         ... doug
```

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [July 28, 2009, 6:24am UTC](https://www.ruby-forum.com/t/action-placed-in-application-controller-doesnt-inherit-down/172726/4 "2009-07-28T06:24:52Z")

</div>

Well, I didn’t understand your explanation much, but let me explain  
myself  
about what I meant by the line

> Requiring the same action for multiple controllers is quite uncommon,

If you are trying to render the _same thing_ on two different routes,  
that  
thing isn’t itself DRY. Use “redirect\_to()” instead.  
If you want to really render the same thing on two different actions,  
that’s  
what partials are for.  
See

> **[Layouts and Rendering in Rails — Ruby on Rails Guides](https://guides.rubyonrails.org/layouts_and_rendering.html#using-partialsfor)**
>
> Layouts and Rendering in RailsThis guide covers the basic layout features of Action Controller and Action View.After reading this guide, you will know: How to use the various rendering methods built into Rails. How to create layouts with multiple...

more details.

What I understood from your text is that “Reading” is common for both  
Main  
and Admin. If the common code is totally database specific, you should  
be  
using a class method of the model instead. And just call it in action  
like,  
say,  
@read\_user = ModelName.custom\_read(params[:something])

Hope that helps. Let me know your thoughts.

2009/7/28 djolley [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> have too many controllers. In this case, I have a ‘main’ controller  
> let it inherit down to both the main and admin controllers. It seems  
> to work just fine. Do you still think that I’m on the wrong track?
> 
> Thanks for the input.
> 
> ```
> ... doug
> 
> ```
> 
> >

–  
-nasa

> **[raining.in](http://its.raining.in)**
>
> Intranet Systems,Dedicated Servers North America,Solarwinds it Asset Management
