<p></p>
<p dir="auto">For a few years we've been slowly refactoring our controllers to use "Resourceful Actions". I think it is worth having an issue here to explain the process, so that other people know what's going on, and how they can help.</p>
<p dir="auto">In Rails you can name your controller actions whatever you like. For example, we have <code class="notranslate">go_public</code> in UsersController and <code class="notranslate">subscribe</code> in ChangesetsController. Resourceful actions (sometimes known as "restful" actions, there's no strict terminology here) are where a controller only has actions from the following list:</p>
<ul dir="auto">
<li>index</li>
<li>show</li>
<li>new</li>
<li>create</li>
<li>edit</li>
<li>update</li>
<li>destroy</li>
</ul>
<p dir="auto">This helps keep our code organised, and therefore easier to maintain. We can be clear about which actions need database write access (create, update, destroy), we know which actions will need a view template and which will iterate over a list. This refactoring allows us to use more <a href="https://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default" rel="nofollow">resourceful routing</a> with fewer customisations.</p>
<p dir="auto">When a controller has additional methods, that usually indicates that we need (at least) one more controller. The first step is to consider what kind of action you are looking at:</p>
<ul dir="auto">
<li>Is it showing a list of items? It's probably an <code class="notranslate">index</code> action</li>
<li>Is it showing just one item? It's probably a <code class="notranslate">show</code> action</li>
<li>Is it creating something new? It's probably a <code class="notranslate">create</code> action</li>
<li>etc</li>
</ul>
<p dir="auto">The tricky part is trying to find the right name for the new controller. Here's my guidance, perhaps other people can help clarify further.</p>
<p dir="auto">First, controllers should usually be named after nouns. The actions are verbs, and those verbs should apply to a noun. So for example, with ChangesetsController#subscribe, then instead of using the verb "subscribe" you could use the verb "create" on the noun "subscription". (An alternative might be to "create" a "subscriber", which is also a noun (and changesets do have "subscribers"), but we aren't creating the user here so "creating" a "subscriber" isn't quite accurate).</p>
<p dir="auto">Secondly, you will likely then up with a chain of multiple nouns. So in our example, we have a "subscription" noun and also the "changeset" noun, since we are creating a subscription to the changeset. In general, if one of the nouns is the same as a model, it will be the name of the module. So in <code class="notranslate">app/controllers/changesets/subscriptions_controller.rb</code> we have</p>
<pre class="notranslate"><code class="notranslate">module Changesets
class SubscriptionsController < ApplicationController
</code></pre>
<p dir="auto">(The alternative here would be to have a subscriptions module with various controllers named after our models inside that, which could work, but is harder to navigate for developers).</p>
<p dir="auto">It's worth putting in time and effort to find the right names for the controllers, which is usually trying to find the correct noun(s). This often needs to be discussed with other developers, since <a href="https://x.com/secretGeek/status/72699978680" rel="nofollow">naming things is one of the two hardest problems in computer science</a>. For example it's not obvious what the noun is for "go_public" in UsersController - perhaps we are "updating" the "PublicStatus" of the "User"? Or something else?</p>
<p dir="auto">The other problem is that in English, many nouns can also be used as verbs. "mark", "export", "embed" are examples where they are both verbs and nouns in our context ("feed" and "mine" are also verbs in other contexts!). So sometimes you might find that the action is the same or similar to a noun, but perhaps there is a better noun that makes things clearer. Again, please feel free to ask.</p>
<p dir="auto">If you have any questions or uncertainties about this process, then I'm happy to clarify.</p>
<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />Reply to this email directly, <a href="https://github.com/openstreetmap/openstreetmap-website/issues/5539">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AAK2OLLMFFTPBEJ7MVG7CO32MDFTJAVCNFSM6AAAAABVXDBQCWVHI2DSMVQWIX3LMV43ASLTON2WKOZSHAYDMNRSHA3TMOI">unsubscribe</a>.<br />You are receiving this because you are subscribed to this thread.<img src="https://github.com/notifications/beacon/AAK2OLPSUHGKPHCYTLHPP4D2MDFTJA5CNFSM6AAAAABVXDBQCWWGG33NNVSW45C7OR4XAZNFJFZXG5LFVJRW63LNMVXHIX3JMTHKOSOBUE.gif" height="1" width="1" alt="" /><span style="color: transparent; font-size: 0; display: none; visibility: hidden; overflow: hidden; opacity: 0; width: 0; height: 0; max-width: 0; max-height: 0; mso-hide: all">Message ID: <span><openstreetmap/openstreetmap-website/issues/5539</span><span>@</span><span>github</span><span>.</span><span>com></span></span></p>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/openstreetmap/openstreetmap-website/issues/5539",
"url": "https://github.com/openstreetmap/openstreetmap-website/issues/5539",
"name": "View Issue"
},
"description": "View this Issue on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>