2

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?

Nemoden
  • 8,816
  • 6
  • 41
  • 65

1 Answers1

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.

Community
  • 1
  • 1
Beartech
  • 6,173
  • 1
  • 18
  • 41
  • Thanks. Strange why I didn't get similar answer from a profession with 7 years rails background? (rhetorical question) – Nemoden Jan 13 '14 at 03:05
  • LOL! Did you get the "just don't do it" answer? – Beartech Jan 13 '14 at 03:07
  • Yes. "This is bad because this is bad". – Nemoden Jan 13 '14 at 03:07
  • You can always go into the directory of a rails app on the command line and run `rails c` and find oddities like `"equipment".pluralize` returns `equipment` but `"deer".pluralize` returns `deers` while `"octopus".pluralize` gives you `octopi`. – Beartech Jan 13 '14 at 03:12
  • While "This is bad because it is bad" is true (gotta love circular logic), if the person is truly experienced and answering that for someone who is new, they should really explain it to make it a learning experience. Unless they don't really know themselves, or can't remember, then they should say "search google for 'Rails singular controller name'" and report back to me what you learned. :-) – Beartech Jan 13 '14 at 03:16
  • Yeah, the worst part is I paid him $600 (got automatically charged by oDesk) for a task I would accomplish in PHP or Python in couple of hours (probably 4 as all programmers tend to overestimate self :) ) and not professional consultation (answers like "it's just because it is"). So, really thank you for enlightening. – Nemoden Jan 13 '14 at 03:21
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45079/discussion-between-beartech-and-nemoden) – Beartech Jan 13 '14 at 03:22