从另一个控制器调用方法

时间:2022-12-02 10:23:13

If I've got a method in a different controller to the one I'm writing in, and I want to call that method, is it possible, or should I consider moving that method to a helper?

如果我在一个不同的控制器中有一个方法,我正在写的那个,我想调用那个方法,是否可能,或者我应该考虑将该方法移动到帮助器?

4 个解决方案

#1


45  

You could technically create an instance of the other controller and call methods on that, but it is tedious, error prone and highly not recommended.

您可以在技术上创建另一个控制器的实例并在其上调用方法,但它是单调乏味,容易出错且非常不推荐。

If that function is common to both controllers, you should probably have it in ApplicationController or another superclass controller of your creation.

如果该函数对于两个控制器都是通用的,那么您应该在ApplicationController或您创建的另一个超类控制器中使用它。

class ApplicationController < ActionController::Base
  def common_to_all_controllers
    # some code
  end
end

class SuperController < ApplicationController
  def common_to_some_controllers
    # some other code
  end
end

class MyController < SuperController
  # has access to common_to_all_controllers and common_to_some_controllers
end

class MyOtherController < ApplicationController
  # has access to common_to_all_controllers only
end

Yet another way to do it as jimworm suggested, is to use a module for the common functionality.

另一种像jimworm建议的方法是使用模块来实现通用功能。

# lib/common_stuff.rb
module CommonStuff
  def common_thing
    # code
  end
end

# app/controllers/my_controller.rb
require 'common_stuff'
class MyController < ApplicationController
  include CommonStuff
  # has access to common_thing
end

#2


3  

Try and progressively move you methods to your models, if they don't apply to a model then a helper and if it still needs to be accessed elsewhere put in the ApplicationController

尝试并逐步将方法移动到模型中,如果它们不适用于模型然后是辅助工具,并且如果仍需要在其他地方访问则放入ApplicationController中

#3


0  

If you requirement has to Do with some DB operations, then you can write a common function (class method) inside that Model. Functions defined inside model are accessible across to all the controllers. But this solution does to apply to all cases.

如果您需要对某些数据库操作执行操作,那么您可以在该模型中编写一个公共函数(类方法)。模型内定义的函数可以访问所有控制器。但是这个解决方案适用于所有情况。

#4


0  

I don't know any details of your problem, but maybe paths could be solution in your case (especially if its RESTful action).

我不知道你的问题的任何细节,但也许路径可以解决你的情况(特别是如果它的RESTful行动)。

http://guides.rubyonrails.org/routing.html#path-and-url-helpers

http://guides.rubyonrails.org/routing.html#path-and-url-helpers

#1


45  

You could technically create an instance of the other controller and call methods on that, but it is tedious, error prone and highly not recommended.

您可以在技术上创建另一个控制器的实例并在其上调用方法,但它是单调乏味,容易出错且非常不推荐。

If that function is common to both controllers, you should probably have it in ApplicationController or another superclass controller of your creation.

如果该函数对于两个控制器都是通用的,那么您应该在ApplicationController或您创建的另一个超类控制器中使用它。

class ApplicationController < ActionController::Base
  def common_to_all_controllers
    # some code
  end
end

class SuperController < ApplicationController
  def common_to_some_controllers
    # some other code
  end
end

class MyController < SuperController
  # has access to common_to_all_controllers and common_to_some_controllers
end

class MyOtherController < ApplicationController
  # has access to common_to_all_controllers only
end

Yet another way to do it as jimworm suggested, is to use a module for the common functionality.

另一种像jimworm建议的方法是使用模块来实现通用功能。

# lib/common_stuff.rb
module CommonStuff
  def common_thing
    # code
  end
end

# app/controllers/my_controller.rb
require 'common_stuff'
class MyController < ApplicationController
  include CommonStuff
  # has access to common_thing
end

#2


3  

Try and progressively move you methods to your models, if they don't apply to a model then a helper and if it still needs to be accessed elsewhere put in the ApplicationController

尝试并逐步将方法移动到模型中,如果它们不适用于模型然后是辅助工具,并且如果仍需要在其他地方访问则放入ApplicationController中

#3


0  

If you requirement has to Do with some DB operations, then you can write a common function (class method) inside that Model. Functions defined inside model are accessible across to all the controllers. But this solution does to apply to all cases.

如果您需要对某些数据库操作执行操作,那么您可以在该模型中编写一个公共函数(类方法)。模型内定义的函数可以访问所有控制器。但是这个解决方案适用于所有情况。

#4


0  

I don't know any details of your problem, but maybe paths could be solution in your case (especially if its RESTful action).

我不知道你的问题的任何细节,但也许路径可以解决你的情况(特别是如果它的RESTful行动)。

http://guides.rubyonrails.org/routing.html#path-and-url-helpers

http://guides.rubyonrails.org/routing.html#path-and-url-helpers