How to call methods of another controller

ruby-on-rails, ruby-on-rails-3

Solution

You could implement a module, and include it in your Controller.

Let's call this module "Products Helper":

# In your app/helpers
# create a file products_helper.rb
module ProductsHelper

  def products_list(product_id)
    catalague = Catalagues.where(id: product_id).first
    render :json => catalague
  end

end

And then, in the controllers you need to use this method:

class Site::CataloguesController < ApplicationController
  include ProductsHelper

  respond_to :js, :html

  def index
    products_list(your_id) # replace your_id with the corresponding variable
  end
end

Problem

I need to call methods from another controller. What is the best way? For example: catalogues_controller.rb ``` class Site::CataloguesController < ApplicationController respond_to :js, :html def index produc_list # call method other controller end end ``` other_controller.rb ``` class OtherController < ApplicationController respond_to :js, :html def produc_list myObj = Catalagues.find(params[:id]) render :json => myObj end end ```

Original source

Related problems