rails-style-guide icon indicating copy to clipboard operation
rails-style-guide copied to clipboard

Controller action ordering

Open mollerhoj opened this issue 3 years ago • 11 comments

The default order of controller actions in rails scaffolds are:

index show new edit create update destroy

I often see controllers where this order is not followed. For consistency's sake, I'd like to have a rubocop to enforce this ordering of the public methods in my controllers.

As a side note: We strive towards never using any other actions in our controllers (https://www.youtube.com/watch?v=HctYHe-YjnE)

mollerhoj avatar Sep 11 '21 10:09 mollerhoj

I'd love to see a confirmation that this order is followed by real-world Rails apps.

pirj avatar Sep 11 '21 13:09 pirj

Just for the sake of discussion, another possible way of ordering could be to follow CRUD, e.g.:

new create index show edit update destroy

andyw8 avatar Sep 11 '21 13:09 andyw8

Also, as this Stack Overflow post says, some people may prefer to group the new/create and edit/update pairs together.

andyw8 avatar Sep 11 '21 13:09 andyw8

I honestly like the CRUD order better, but consistency is my no. 1 priority. (I really just want to see a rubocop, but they refered me to this guide).

mollerhoj avatar Sep 12 '21 13:09 mollerhoj

The default order of controller actions in rails scaffolds are:

I agree with the default order if this rule is needed. I think it's better to follow the scaffold order. This is the official starting point of Rails.

koic avatar Sep 13 '21 02:09 koic

What do you think of a soft wording for the guideline? e.g.

Use the default order of resourceful actions in your controller. Feel free to intermingle them with additional non-resourceful actions deliberately.

pirj avatar Sep 16 '21 19:09 pirj

I don't think the default order is logical - How about "Use a consistent order of resourceful actions in your controller"

mollerhoj avatar Sep 16 '21 20:09 mollerhoj

Feel free to intermingle them with additional non-resourceful actions deliberately.

This feels like it encourages non-resourceful actions, rather than just permitting them. How about:

Use a consistent order for the resourceful actions in your controller. Non-resourceful actions can be interspersed where needed.

andyw8 avatar Sep 16 '21 20:09 andyw8

The default order of controller actions in rails scaffolds are: index show new edit create update destroy

Yes, and the order is grouped by HTTP method.

However, the order displayed at http://localhost:3000/rails/info/routes is different: index create new edit show update destroy

ydakuka avatar Dec 11 '23 18:12 ydakuka

Since it's proving difficult to reach an agreed ordering, perhaps the guideline should just be to use a consistent ordering.

A cop can then be written to allow the developer to configure their preferred order (without any default).

andyw8 avatar Dec 11 '23 22:12 andyw8

A cop can then be written to allow the developer to configure their preferred order (without any default).

The cop has been already written.

I believe the issue remains open due to this comment.

ydakuka avatar Dec 12 '23 18:12 ydakuka