Search code examples
ruby-on-railsruby-on-rails-4rails-4-2-1

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?


Solution

  • 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.