且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

控制器和Rails中的动作之间有什么区别?

更新时间:2022-11-30 22:18:52

控制器只是 Ruby类文件,其中包含一系列实例方法

Controllers are just Ruby Class files which have a series of instance methods inside

基本说明

Rails控制器基本上是保存动作(方法)的文件

Rails controllers are basically files where actions (methods) are kept

每次访问Rails应用时,您都在向系统发送请求。 Rails route 内部的各种技术均要求执行某些 action 动作,您的代码可以使用传递的数据来执行某些操作操作(因此得名)。动作保存在控制器内部,以提供应用程序结构

Each time you access a Rails app, you're sending a request to the system. The various technologies inside Rails route that request to a certain action, where your code can use the passed data to perform some sort of action (hence the name). The actions are kept inside controllers to give the application structure

因此,如果您访问 http ://yourapp.com/users/new ,它告诉Rails在 users中加载 new 方法控制器。您可以在控制器中进行任意数量的操作,但是您必须告诉Rails routes 系统它们在那里,否则将无法访问

So if you access http://yourapp.com/users/new, it tells Rails to load the new method in the users controller. You can have as many actions in the controllers as you want, but you have to tell the Rails routes system they are there, otherwise they won't be accessible

正确的解释

滑轨 Controllers 只是Ruby类,存储一系列操作

Rails Controllers are just Ruby Classes, storing a series of actions

操作(实例方法)对传递的数据起作用( params )创建对象可以传递给模型,也可以在其他方法中使用

The "actions" (instance methods) work on passed data (params) to create objects that can either be passed to the model, or used inside other methods

每当您向Rails发送请求(访问URL)时,它首先使用 ActionDispatch 中间件,将您的请求发送到正确的Class( controller )实例方法( action ),然后您的代码会对这些数据进行处理

Whenever you send a request to Rails (access a URL), it first uses the ActionDispatch middleware to send your request to the correct Class (controller) instance method (action), and then your code does something with that data

您作为开发人员的工作是将正确的控制器与正确的模型连接起来,并在正确的时间向用户提供正确的数据

Your job as a dev is to connect the right controllers with the right models, presenting the right data the user at the right time