你把元编程放在 Rails 的什么地方?

发布于 2025-01-08 08:01:30 字数 406 浏览 3 评论 0原文

假设我想向 ActionView::Helpers::FormBuilder 添加一些方法,如本博客中的示例 http://blog.lrdesign.com/tag/adding-methods/ 这些方法特定于该项目,不属于 gem。

我见过元编程塞满了从 application.rb 到控制器类之后的各种地方。我希望下一个程序员(或者 6 个月后的我自己)不要绞尽脑汁地想知道这些新方法从何而来。或者为什么 text_field 方法没有按您期望的方式工作。

这些方法的最佳位置、文件结构和名称间距是什么,以便 Rails 可以加载它们,并且下一个 Rails 程序员可以了解发生了什么。

谢谢

Lets say I want to add a few methods to ActionView::Helpers::FormBuilder like the examples in this blog http://blog.lrdesign.com/tag/adding-methods/ The methods are specific to this project and don't belong in a gem.

I've seen metaprogramming jammed into all kinds of places from the application.rb to after controller classes. I want the next programmer ( or myself in 6 months ) to not be pulling their hair out wondering where these new methods came from. Or why the text_field method is not working the way you expect it to.

What is the best place, file structure, and name spacing for these methods so Rails can load them up and the next rails programmers can understand what's going on.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一花一树开 2025-01-15 08:01:30

在 /lib/ 中声明模块并在 application.rb 中引用它们。

Declare modules inside /lib/ and require them in application.rb.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文