I have a DashboardController
. This is the page where the user views their profile, settings, etc.
It makes sense for me that dashboard be singular.
For now, the dashboard is also the root. So long as the user is logged in, the user can access their dashboard. A user can have many children
, so I have added an action for adding a child to the controller.
I have defined the routes so:
resource :dashboard, only: [:show], path: 'dashboard' do
post :create_child, on: :collection
end
resolve("Dashboard") { [:dashboard] }
root "dashboard#show"
The root shows as singular but the path to the controller is plural, and so is the nested route.
create_child_dashboard POST /dashboard/create_child(.:format) dashboards#create_child
dashboard GET /dashboard(.:format) dashboards#show
root GET / dashboard#show
I want the URLs to be singular. How do I ensure that?
The root shows as singular but the path to the controller is plural
This is actually exactly how it's supposted to be. Rails does not differentiate between the naming of controllers for normal or singular resources. They are always plural.
While you may not agree with this it's easier to just have one single naming convention for all controllers. And I would really encourage you to learn the conventions before imposing your own ideas on them.
That said it's pretty debatable if "dashboard" really is a resource at all. You're not actually creating or updating it and it really just serves as a splash page and grouping for a set of loosely related routes. You should really just define it as a scope or namespace (if you also want module nesting).
Beyond that you're not actually defining a nested resource but rather just creating a non-idomatic route. A nested resource in Rails is created by POSTing to the nested collection path - for example:
POST /users/1/pets -> pets#create
POST /user/pets -> pets#create
The later example is with a singular parent resource - it being implied that the user is the current user (which also makes the nesting a bit redundant).
If you ever add create_
, update_
or similiar verbs in the path of a Rails route you're doing it wrong. And a controller should not be responsible for both a resource and its children.
So I would instead go for something like:
Rails.application.routes.draw do
root "dashboard#show"
scope :dashboard do
get to: "dashboard#show" # /dashboard
# if child is also singular
resource :child
# if plural
resources :children
end
end