轨道助手 [英] Helpers in rails

查看:38
本文介绍了轨道助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始接触 Rails,还有很多东西需要学习,所以我可能会比平常更频繁地在 Stackoverflow 上询问初学者 Rails/Ruby 问题.

I'm just starting out in Rails and there's a lot I still need to learn so I'm likely to be on Stackoverflow more often than normal asking beginner Rails / Ruby questions.

我只是想弄清楚 Helpers 在 Rails 中是如何工作的.从我目前所见,Helpers 旨在与 Views 一起使用,而不是与您的 Controllers 一起使用.

I'm just trying to figure out how Helpers work in Rails. From what I've seen so far, Helpers are intended to be used with Views and not so much with your Controllers.

但是,我想创建一个简单的函数来验证 params 中给出的用户输入(检查是否定义了某些参数,并可选择检查它们的值是否有效).

However I would like to make a simple function that will validate the user input given in params (check if certain params are defined and optionally check if their value is valid).

谁能向我解释什么是实现这一点的最佳方式?(请记住,我希望在许多不同的控制器中使用它,因此它应该是全局可用的.)

Can anyone explain to me what would be the best way of implementing this? (Keeping in mind that I will want to use this in many different controllers so it should be globally available.)

我还注意到,默认情况下,Rails 不会在主应用程序文件夹中生成 lib 文件夹.开发人员是将他们的库放在主文件夹中的 app 文件夹之外,还是 Rails 使用库的方式不同?

I also noticed that by default Rails does not generate a lib folder in the main application folder. Are developers to place their libs outside the app folder in the main folder, or does Rails use libraries differently?

推荐答案

关于您的验证问题,这取决于您要验证的内容.

With regards to your validation issue, it depends on what you are validating.

如果数据构成了问题域中的对象,也称为模型,那么您应该使用 ActiveModel 的内置验证器.这可能是您应该做的,但是在不知道确切问题的情况下很难说.请参阅Rails 验证指南.您可以通过问自己需要验证的数据在您获取后是否将其存储来判断是否是这种情况.如果是这样,它绝对是一个模型.此类数据的一个示例是从浏览器表单发送到 Rails 的博客文章的标题和文本字段.

If the data makes up objects from your problem domain, also known as models, then you should use the built in validators from ActiveModel. This is probably what you should do, but its hard to say without knowing the exact problem. See the Rails Guides on Validations. You can tell if this is the case by asking yourself if the data that needs validation will be stored after you get it. If so, its most definitely a model. An example of this kind of data would be the title and text fields of a blog post being sent to Rails from a browser form.

如果数据对您的模型来说是第三级的,或者特定于演示文稿,那么您应该可以使用助手.您注意到助手主要在视图中使用,虽然这是真的,但没有什么可以阻止您在控制器中使用它们,您只需要声明您将使用 ActiveController#helper方法.在 ApplicationController 类中,很多开发者都会把 helper :all 放在所有控制器中.一旦需要代码一次,它就不会真正对性能造成太大的影响.

If the data is something tertiary to your models, or specific to presentation, then you should be fine using helpers. You noticed that helpers are used mostly in the views, and although this is true, theres nothing stopping you from using them in the controllers, you just have to declare that you will use them using the ActiveController#helper method. Inside the ApplicationController class, a lot of devs will put helper :all to just include all the helpers in all the controllers. Once the code has been required once, it doesn't really incur that big a performance hit.

请注意,几乎所有传入的数据都可以使用模型进行建模.Rails 世界中的一个大思想流派赞同 Fat Model 的想法.人们说在模型中放置尽可能多的代码而在控制器中放置尽可能少的代码可以正确分离关注点并导致更易于维护的代码.这表明,即使您不认为传入数据是可建模的(从某种意义上说,您可以创建一个模型来表示它),您也应该尝试使其成为一个模型并封装围绕验证它的逻辑.但是,您可能会发现创建辅助函数更快,而且两者都可以.

Do note that almost all incoming data can be modeled using a model. A big school of thought in the Rails world subscribes to the Fat Model idea. People say that putting as much code as possible in the model and as little in the controller as possible separates concerns properly and leads to more maintainable code. This suggests that even if you don't think the incoming data is modelable (in the sense that you can create a model to represent it), you should try to make it a model and encapsulate the logic around validating it. However, you may find that making a helper function is faster, and either will work.

您验证用户输入的想法很好.我的感觉是,由于您是 Rails 新手,您习惯于自己做这些事情,但这在这里并不完全适用.在 Rails 世界中,很多常见的东西,比如验证,都是由框架处理的.您不必检查 params 数组中是否存在,而是在模型上调用 validates_presence_of 并让 Rails 将错误输出给用户.从长远来看,如果您让框架按照其设计的目的去做,事情就会变得更容易.

Your notion of validating user input is a good one. I get the feeling that as you are new to Rails you are used to doing these things yourself, but that doesn't quite apply here. In the Rails world, a lot of the common stuff like validations is handled by the framework. You don't have to check for presence in the params array, instead you call validates_presence_of on the model and let Rails spit the error out to the user. It makes things easier in the long run if you let the framework do what it is designed to.

关于您关于 lib 文件夹的问题,这并不重要.您可以将其他支持文件和库放在根目录的 lib 文件夹中,它们将可用于您的应用程序(app 文件夹中的文件).您还可以选择将您的代码抽象为插件或 gem 并以这种方式包含它,很多人选择这样做.我对此的建议是在深入研究之前阅读 gem 和插件的概念.

With regards to your question about the lib folder, it doesn't really matter. You can put miscellaneous support files and libraries in the lib folder in the root directory and they will be available for use in your application (the files in the app folder). You can also choose to abstract your code into a plugin or a gem and include it that way, which a lot of people opt to do. My suggestion for this would be to read up on the notion of gems and plugins before diving in.

这篇关于轨道助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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