[英]Ruby On Rails : What's the correct way to naming a nested controller and view
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 : 我使用脚手架创建了一些控制器,这是命名嵌套资源rails g scaffold schedule/event
输出的正确方法:
or rails d scaffold schedules/event
或rails d scaffold schedules/event
Thanks, and if you can gimme some references and why you choices an answer between those two, it'll be helful:) 谢谢,如果您能给我一些参考,以及为什么在这两个之间选择一个答案,那将是非常有益的:)
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. schedules/event
读起来更像传统的Rails路线,所以我个人建议这样做。 But I don't think it should impact any functional aspect of the app. 但是我认为这不会影响该应用程序的任何功能方面。
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
的输出应类似于:
❯ 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. 注意app/controllers/schedule/events_controller.rb
以及只有实体名称是复数形式的情况。
Rails uses an inflector to generate the plural form, if you skip this process some things might not work as you expect. Rails使用变形器生成复数形式,如果您跳过此过程,则某些事情可能无法按预期工作。
As a general rule always use singular when scaffolding. 通常,脚手架时始终使用单数形式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.