CodeCrack
CodeCrack

Reputation: 5353

proper way to define resources for singular name controller and model in rails?

if I have articles controller and article model I would create resources: articles in config/routes.rb

but let's say I have a singular controller that I want to use for control panel such as control_panel_controller.rb (not control_panels_controller.rb) and a model that matches that controller control_panel.rb Would I just use resources: control_panel as proper ruby convention or I would have to do it some other way?

Upvotes: 0

Views: 1524

Answers (1)

vpsz
vpsz

Reputation: 457

You can specify singular routes and override the default controller name that is expected.

Here is how I set up my home page:

resource :home, only: [:show], controller: 'home'

class HomeController < ApplicationController
...
end

This gives a helper called 'home_path' that generates the '/home' URL.

Take a look at the Rails routing guide; it does cover these options they're just not super obvious.

Edit: btw, your model names are totally independent of the controllers/routes. Of course it's good convention to make them match where possible, but often times as your app grows there will be many models or service objects that don't directly correspond to a controller with CRUD actions for it.

Upvotes: 1

Related Questions