Wojciech Bednarski
Wojciech Bednarski

Reputation: 6373

Singleton controller in Rails 4.2

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?

Upvotes: 0

Views: 1551

Answers (2)

Eugene Petrov
Eugene Petrov

Reputation: 1598

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

Upvotes: 0

joshua.paling
joshua.paling

Reputation: 13952

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.

Upvotes: 2

Related Questions