# Running code in another controller action

**URL:** <https://www.ruby-forum.com/t/running-code-in-another-controller-action/225130>\
**Category:** Rails\
**Created:** [December 16, 2012, 10:25am UTC](https://www.ruby-forum.com/t/running-code-in-another-controller-action/225130 "2012-12-16T10:25:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [December 16, 2012, 10:25am UTC](https://www.ruby-forum.com/t/running-code-in-another-controller-action/225130/1 "2012-12-16T10:25:42Z")

</div>

I am doing a partial render in the view from one controller. Here is the  
code -\> \<%= render :template =\> ‘restaurants/new’ %\>. I want the  
other  
controller new action code to do it. Is there a way to call an action  
in  
another controller ? I don’t want to duplicate the same code in two  
controllers.

---

<div class="post-metadata">

**Author:** ![rubyrookie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4af34b/32.png) [@rubyrookie](https://www.ruby-forum.com/u/rubyrookie)\
**Post date:** [December 16, 2012, 11:22am UTC](https://www.ruby-forum.com/t/running-code-in-another-controller-action/225130/2 "2012-12-16T11:22:22Z")

</div>

On Dec 16, 9:24am, rubyrookie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am doing a partial render in the view from one controller. Here is the  
> code → \<%= render :template =\> ‘restaurants/new’ %\>. I want the other  
> controller new action code to do it. Is there a way to call an action in  
> another controller ? I don’t want to duplicate the same code in two  
> controllers.

In a nutshell, no. If you need to do this you might want to look at:

- putting the shared code into a module
- putting it into a common ancestor of the 2 classes
- moving the functionality into a helper that both controllers can  
share
- look at cells gem, which is one way of having reusable units of view

- code

Fred

---

<div class="post-metadata">

**Author:** ![rubyrookie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4af34b/32.png) [@rubyrookie](https://www.ruby-forum.com/u/rubyrookie)\
**Post date:** [December 17, 2012, 4:39am UTC](https://www.ruby-forum.com/t/running-code-in-another-controller-action/225130/3 "2012-12-17T04:39:55Z")

</div>

Thanks . I am experiment by putting common code in a helper . How Do i  
access it from my controller. I tried to do  
ApplicationHelper::base64\_url\_decode in the ApplicationController bu I  
am  
getting method not defined. Any ideas? 🙂

module ApplicationHelper

def base64\_url\_decode str  
encoded\_str = str.gsub(’-’,’+’).gsub(’\_’,’/’)  
encoded\_str += ‘=’ while !(encoded\_str.size % 4).zero?  
Base64.decode64(encoded\_str)  
end

def decode\_data str  
encoded\_sig, payload = str.split(’.’)  
data = ActiveSupport::JSON.decode base64\_url\_decode(payload)  
end  
end

---

<div class="post-metadata">

**Author:** ![rubyrookie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4af34b/32.png) [@rubyrookie](https://www.ruby-forum.com/u/rubyrookie)\
**Post date:** [December 17, 2012, 5:13am UTC](https://www.ruby-forum.com/t/running-code-in-another-controller-action/225130/4 "2012-12-17T05:13:13Z")

</div>

Put it in application\_controller.rb
