Will Dashboard become Dashboards when creating a controller?

The user controller is called UserController for simple reasons, because it controls several users, but in reality it does not change only a couple of parameters that are specific to a particular user?

Therefore, it would be most logical for me that the names of the Dashboard controllers should be named DashboardsController, since it controls several dashboards on multiple PCs and because of a small change in a specific spesific field after rendering the page, therefore it should be called "Personal Panels". Like Users are called Users, not User ...

This brings me back to the starting point. How can I decide what to choose; plural or singular? The toolbar is only one specific thing in the application, while users do not really control only their own, but again Dashboards are all different when user names are displayed on the page, as well as special user tags.

What would be the best practice?

+4
source share
3 answers

The rails convention is to define controller names using the plural (for example, dashboards). It is not set in stone, and you can change it according to your needs, but if you ask what your best practice is, it is best to stick to the rails agreement, especially if you are just starting out with rails. It will make your life a lot easier.

If you really want to use a specific term for your controller (for example, / dashboard), you can specify this in the routes.rb file (it is assumed that you use RESTful routes and define the control panel as a resource)

resource :dashboard, :controller => 'dashboard' 

Also note that if you decide to go with a singular, you will need to configure the helper methods created by the rails for RESTful routes, so

  dashboards_path # will no longer work, you must use dashboard_path 
+2
source

I am using a presenter template for toolbars. Toolbars are a hybrid of objects, and it's hard to think of it as a show or index page. I will create a virtual object to process requests and aggregate related objects. It really clears the controller code, even if you decide to go with the DashboardsController.

But I would recommend adding to the resources. A route museum.

 collection do get :dashboard end 

And then under UserController add an action bar

 def dashboard @presenter = UserDashboardPresenter.new(user) end 

Use @presenter in the view.

You will find more in the presenter template if you search for it. This is great for caching, code reuse, testing, and performance improvement.

+3
source

Using plural names for controllers is just one of the Rails rules. Should you deviate from the convention? I think it’s better if you stick to it.

Numerous names usually sound more natural, as you mentioned Users. I'm sure your DashboardsController will have more than one action, so it will return to the idea of ​​pluralism.

My advice adheres to a multiple agreement.

+1
source

All Articles