最佳实践:放置从视图调用的函数的位置

时间:2022-07-24 16:04:56

I am new to Ruby on Rails and the whole MVC style of doing things. This question isn't so much technical as it is conceptual. Say I have some function myFunction that I wish to have a user execute from the View by pushing a button. Should I have this function in thew Model, or the controller?

我是Ruby on Rails的新手,也是整个MVC的做事方式。这个问题不是技术问题,而是概念问题。假设我有一些函数myFunction,我希望通过按下按钮让用户从View执行。我应该在模型或控制器中使用此功能吗?

3 个解决方案

#1


1  

If the function relates clearly to a particular model e.g. "placing an Order", "authenticating a User" then place it on the appropriate model and add a small amount of controller code to create/retrieve the appropriate model and call the method.

如果该功能与特定型号明确相关,例如“下订单”,“验证用户”然后将其放在适当的模型上并添加少量控制器代码以创建/检索适当的模型并调用该方法。

If it doesn't obviously belong on one of your models then you may want to create a separate class or module for it, and again add a small amount of controller code.

如果它显然不属于您的某个模型,那么您可能需要为其创建单独的类或模块,并再次添加少量控制器代码。

Do some Googling for "skinny controllers and fat models", this is the approach generally favoured for Rails projects. e.g. see this (old, but still useful) post from Jamis Buck, or this more recent post.

做一些谷歌搜索“瘦瘦的控制器和胖模型”,这是Rails项目普遍青睐的方法。例如从Jamis Buck看到这篇(旧的,但仍然有用)的帖子,或者这篇最近的帖子。

#2


0  

It should goes to controller's method which is using models to do some business logic (not necessary connected with persistance). Probably, you should also read about RESTful apps and routing (there is a lot of guides about it).

它应该转向控制器的方法,该方法使用模型来做一些业务逻辑(不必与持久性连接)。或许,您还应该阅读有关RESTful应用程序和路由的信息(有很多关于它的指南)。

#3


0  

Controller's are just expected to have the Input and Output which is the request parameters and the response/redirect. So according to you if a user clicks a button and myFunction should come into picture. Put the call to myFunction inside the view helper and the function myFunction inside the model. So your View is also clean from code and the Skinny controller and Fat models come into picture

控制器只需要输入和输出,这是请求参数和响应/重定向。所以根据你的说法,如果用户点击一个按钮,myFunction应该会出现。将调用myFunction放在视图助手中,将函数myFunction放入模型中。所以你的View也很干净,代码和Skinny控制器和Fat模型都会出现

#1


1  

If the function relates clearly to a particular model e.g. "placing an Order", "authenticating a User" then place it on the appropriate model and add a small amount of controller code to create/retrieve the appropriate model and call the method.

如果该功能与特定型号明确相关,例如“下订单”,“验证用户”然后将其放在适当的模型上并添加少量控制器代码以创建/检索适当的模型并调用该方法。

If it doesn't obviously belong on one of your models then you may want to create a separate class or module for it, and again add a small amount of controller code.

如果它显然不属于您的某个模型,那么您可能需要为其创建单独的类或模块,并再次添加少量控制器代码。

Do some Googling for "skinny controllers and fat models", this is the approach generally favoured for Rails projects. e.g. see this (old, but still useful) post from Jamis Buck, or this more recent post.

做一些谷歌搜索“瘦瘦的控制器和胖模型”,这是Rails项目普遍青睐的方法。例如从Jamis Buck看到这篇(旧的,但仍然有用)的帖子,或者这篇最近的帖子。

#2


0  

It should goes to controller's method which is using models to do some business logic (not necessary connected with persistance). Probably, you should also read about RESTful apps and routing (there is a lot of guides about it).

它应该转向控制器的方法,该方法使用模型来做一些业务逻辑(不必与持久性连接)。或许,您还应该阅读有关RESTful应用程序和路由的信息(有很多关于它的指南)。

#3


0  

Controller's are just expected to have the Input and Output which is the request parameters and the response/redirect. So according to you if a user clicks a button and myFunction should come into picture. Put the call to myFunction inside the view helper and the function myFunction inside the model. So your View is also clean from code and the Skinny controller and Fat models come into picture

控制器只需要输入和输出,这是请求参数和响应/重定向。所以根据你的说法,如果用户点击一个按钮,myFunction应该会出现。将调用myFunction放在视图助手中,将函数myFunction放入模型中。所以你的View也很干净,代码和Skinny控制器和Fat模型都会出现