ruby on rails 中的控制器和动作有什么区别? [英] What's the difference between controllers and actions in ruby on rails?

查看:19
本文介绍了ruby on rails 中的控制器和动作有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我 ruby​​ on rails 中控制器和动作的区别?

Can anybody tell me the difference between controllers and actions in ruby on rails?

我从官方 Rails 指南中获取了这个定义:

I fetched this definition from the official rails guide:

控制器的目的是接收应用程序的特定请求.路由决定哪个控制器接收哪个请求.通常,每条线路都不止一条控制器,不同的路由可以由不同的动作提供服务.每个动作的目的是收集信息以将其提供给视图.

A controller's purpose is to receive specific requests for the application. Routing decides which controller receives which requests. Often, there is more than one route to each controller, and different routes can be served by different actions. Each action's purpose is to collect information to provide it to a view.

我很困惑.请尽量简单,因为我是新手!

I am confused. Please, make it as simple as possible since I am newbie!

谢谢!

推荐答案

控制器只是 Ruby Class 包含一系列 实例方法里面

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,您的代码可以使用传递的数据执行某种 action(因此名字).这些动作被保存在 controllers 中以给出应用程序结构

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

正确解释

Rails 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 中间件将您的请求发送到正确的类(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

这篇关于ruby on rails 中的控制器和动作有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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