0

I wonder how to create singleton controller in Rails 4.2.

For example rails g scaffold Dashboard will generate dashboards_controller witch in my case has no sense because I need only one Dashboard so dashboard_controller is the thing I need.

I see there is an option -c to specify controller name, however I bet there was something like --singleton but is gone now.

So, the question is, should I use -c to override controller name or the "new Rails way" is to create plural controllers names, like dashboards_controller and then use router to point it to dashboard URL?

Wojciech Bednarski
  • 6,033
  • 9
  • 49
  • 73

2 Answers2

2

I don't know how to do it using a generator, but it's easy enough to generate with a plural name and then change it to singular manually.

Your route will be something like:

resource :dashboard, controller: 'dashboard', :only => ['show']

Your controller class should be renamed to DashboardController and the file name itself to dashboard_controller.rb. The view folder that holds your view files should also be singular - app/views/dashboard

The "Rails Way" is to go with plural controller names by default, but it's fine to use singular controller names when they make sense - which they certainly do in this case.

joshua.paling
  • 13,762
  • 4
  • 45
  • 60
  • I found the flag for it `-c` it let me specify controller name, the thing is that I'm just looking for confirmation if this is OK to use singular names in Rails. Official guidelines says controller should be plural but they ship Rails with `application_controller` which is singular so I guess it is OK. Thanks for answer! – Wojciech Bednarski May 18 '15 at 23:28
0

rails g controller dashboard seems what you are looking for.

$ rails g controller dashboard
      create  app/controllers/dashboard_controller.rb
      invoke  erb
      create    app/views/dashboard
      invoke  test_unit
      create    test/controllers/dashboard_controller_test.rb
      invoke  helper
      create    app/helpers/dashboard_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/dashboard.coffee
      invoke    scss
      create      app/assets/stylesheets/dashboard.scss
Eugene Petrov
  • 1,578
  • 1
  • 10
  • 22