What are drawbacks of naming controllers in singular form is Rails?

喜夏-厌秋 提交于 2020-01-15 05:27:07

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/21083020/what-are-drawbacks-of-naming-controllers-in-singular-form-is-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!