如何从Rails应用程序调用Rails可安装引擎方法?

时间:2015-04-09 04:42:31

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2

可安装的发动机控制器方法:

module ServiceApi
  module Api
    module V1
      class RequestorController < ApplicationController
        def get_details(query_parameters)
          #some code here
        end
      end
    end
  end
end

Rails app Controller从可安装引擎调用get_details(params)方法

ServiceApi::Api::V1::RequestorController.new.get_details(params)

1 个答案:

答案 0 :(得分:0)

在我看来,您似乎不是在寻找控制器操作,而是在寻找一个 helper方法。例如,控制器动作不带参数。

如果您仍然希望按说明将其保留在控制器中,那很好,但是您可以将其提取到一个帮助器中,并将该帮助器包含在要使用它的每个控制器中。

例如,

# [engine]/app/helpers/requestor_helper.rb

module ServiceApi::Api::V1::RequestorHelper
  def get_details(query_parameters)
    # code
  end
end

,然后在您的控制器中

include ServiceApi::Api::V1::RequestorHelper

如果您想要的是module,则无需滥用课程。