Running code in another controller action

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.

On Dec 16, 9:24am, rubyrookie [email protected] 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

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? :slight_smile:

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

Put it in application_controller.rb