将控制器命名为单数形式的Rails有什么缺点? [英] What are drawbacks of naming controllers in singular form is Rails?

查看:73
本文介绍了将控制器命名为单数形式的Rails有什么缺点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在Rails中我们遵循惯例.我们应该以复数形式命名控制器. 最近,我聘请了一名自由职业者来帮助我在Rails中完成应用程序的一部分,因为我真的是这个框架和ruby的新手. 我有一个PortfolioController-感觉很正确,因为投资组合是条目的容器(谁说我有投资组合"?). 自由职业者说这是不对的,如果不遵循约定,我会遇到麻烦,并将其重命名为PortfoliosController.我问了好几次,如果我将控制器命名为PortfolioController而不是PortfoliosController,将会遇到什么确切的问题,除了您将遇到问题"之外,我没有得到任何解释.

那么,有人可以告诉我这些问题是什么吗?

解决方案

好吧,最简单的原因是,从事该项目的任何人在编写代码时都可能会以复数形式引用它,然后必须意识到在未指定的"WTF"时间段之后,哦,他们决定不遵循那个控制器中的约定?"而他们试图弄清楚自己在做什么错.同样从语义上讲,您的控制器是投资组合"表中所有投资组合"的控制器.

按代码分类,您将遇到路线问题.由于默认情况下http://my_app/portfolios进入控制器的index动作,因此您必须制作一堆非标准路线.然后,您将显示带有http://my_app/portfolios/1的特定组合,该组合将显示具有1id的组合.因此,请准备在config/routes.rb文件中创建和维护大量自定义路由.对于名称相同的事物,无论是复数还是单数,您都会看到类似的问题,例如equipment,您可以拥有一个设备或多个设备.请参阅:当模型的单数和复数名称相同(例如设备,种类)时,会出现轨道链接路径和路由错误.这不仅使您的路由变得不方便,而且还导致诸如portfolio_pathportfolio_url之类的方法发生冲突.

I know in Rails we follow conventions. And we should name controllers in plural form. Recently I hired a freelancer to help me with one part of my application in Rails as I'm really new to this framework and ruby. I had a PortfolioController - this is just feels right because portfolio is a container for entries (who says "I have portfolios"?). The freelancer said it's not right and I will have troubles not following the convention and renamed it to PortfoliosController. I asked several times what are the exact problems I will have if I name my controller PortfolioController rather than PortfoliosController and I didn't get any explanation other than "You will have problems".

So, can anybody tell me what those problems are?

解决方案

Well, the simplest reason would be that anyone else who works on that project will probably refer to it in the plural while working on the code, then have to realize "oh, they decided to not follow convention in that one controller" after an unspecified time period of "WTF?" while they try to figure out what they are doing wrong. Also semantically, your controller is a controller of ALL the Portfolios in the Portfolio table.

Code-wise you will have issues with routes. You will have to craft a bunch of non-standard routes because http://my_app/portfolios goes to the index action of the controller by default. You then show a particular portfolio with http://my_app/portfolios/1 which will show you the portfolio with the id of 1. So be prepared to create and maintain a host of custom routes in your config/routes.rb file. You see similar problems with things that have names that are the same whether plural or singular like equipment where you can have one piece of equipment or many pieces of equipment. See this: rails link path and routing error when model singular and plural name are the same (e.g. equipment, species). Not only is it making your routes wonky, it is causing conflicts in methods like portfolio_path or portfolio_url.

这篇关于将控制器命名为单数形式的Rails有什么缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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