0

you might that's a silly question though. But for me as a beginner, it was quite needed.

I've created some controller using scaffold and which is the correct way of naming nested resource rails g scaffold schedule/event output :

  • models/schedule.rb
  • models/schedule/event.rb
  • controllers/schedule/events_controllers.rb
  • views/schedule/events/

or rails d scaffold schedules/event

  • models/schedules.rb
  • models/schedules/event.rb
  • controllers/schedules/events_controllers.rb
  • views/schedules/events/

Thanks, and if you can gimme some references and why you choices an answer between those two, it'll be helful:)

2 Answers2

1

As far as I know the plurality of namespace doesn't matter, so both are technically correct.

schedules/event reads more like a traditional Rails route so I'd recommend that personally. But I don't think it should impact any functional aspect of the app.

user2490003
  • 10,706
  • 17
  • 79
  • 155
1

Model names have to always be singular, controllers plural. Otherwise you might encounter problems when routing. Also, in your question, the output of rails g scaffold schedule/event should be something like:

❯ rails g scaffold schedule/event
Running via Spring preloader in process 1938
      invoke  active_record
      create    db/migrate/20180704090256_create_schedule_events.rb
      create    app/models/schedule/event.rb
      create    app/models/schedule.rb
      invoke    test_unit
      create      test/models/schedule/event_test.rb
      create      test/fixtures/schedule/events.yml
      invoke  resource_route
       route    namespace :schedule do
    resources :events
  end
      invoke  scaffold_controller
      create    app/controllers/schedule/events_controller.rb
      invoke    erb
      create      app/views/schedule/events
      create      app/views/schedule/events/index.html.erb
      create      app/views/schedule/events/edit.html.erb
      create      app/views/schedule/events/show.html.erb
      create      app/views/schedule/events/new.html.erb
      create      app/views/schedule/events/_form.html.erb
      invoke    test_unit
      create      test/controllers/schedule/events_controller_test.rb
      invoke    helper
      create      app/helpers/schedule/events_helper.rb
      invoke      test_unit
      invoke    jbuilder
      create      app/views/schedule/events/index.json.jbuilder
      create      app/views/schedule/events/show.json.jbuilder
      create      app/views/schedule/events/_schedule_event.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/schedule/events.coffee
      invoke    scss
      create      app/assets/stylesheets/schedule/events.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.scss

Notice app/controllers/schedule/events_controller.rb and how only the entity name is plural.

Rails uses an inflector to generate the plural form, if you skip this process some things might not work as you expect.

As a general rule always use singular when scaffolding.

This might give you some insights as well

Alex.U
  • 1,631
  • 2
  • 16
  • 26