如何调用控制器的方法 [英] How to call controller's method

查看:196
本文介绍了如何调用控制器的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与传入请求无关的功能(如get,post等)。



但我想遵循MVC惯例,使它像Model-Controller:




    <在控制器部分中,我将处理来自另一部分的收入请求,决定从哪个模型检索信息,授权,过滤等;




    在Rails中,控制器的操作可以通过路由文件指向的收入请求调用。



    ve创建一个控制器这样:

      class SomeController< ApplicationController 

    def some_action; end

    end

    如何调用从我的应用程序中的任何地方的来自 SomeController



    调用 SomeController.new.some_action 似乎不对我,因为我认为所有的控制器是app对象的一部分。

      SomeController.new。 some_action 

    然而,这不是一个真正的MVC方式。我真的不知道你想要使用它,但可能有一个更好的方法。例如,它可以在模型中完成,因为数据的修改应该不属于控制器。


    I have a functionality that doesn't relate to incoming requests (like get,post,etc).

    But I want to follow MVC convention and make it like Model-Controller:

    • in controller part I will process income requests from another parts, decide from which model it should retrieve information, authorization, filtering and so on;

    • in model part I will store information, validate it, expire it and etc.

    In Rails the controller's actions can be called by income request directed by routes file.

    The problem for me that if I've created a controller like this:

    class SomeController < ApplicationController
    
      def some_action; end
    
    end
    

    How can I call method some_action from SomeController from any place in my application?

    P.S. Calling SomeController.new.some_action doesn't seem right to me, because I think all controllers are parts of app object.

    解决方案

    You can do this by instanciating the controller.

    SomeController.new.some_action
    

    However this is not really an MVC way. I don't really know what you want to use it for, but probably there is a better way. For example it can be done as in the model, as the modification of data should belong there not to a controller.

    这篇关于如何调用控制器的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆