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

查看:101
本文介绍了控制器和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类文件,其中包含一系列<里面的href = http://www.railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/ rel = noreferrer>实例方法

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

操作(实例方法)对传递的数据起作用(< a href = https://stackoverflow.com/questions/6885990/rails-params-explained> 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

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

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