简体   繁体   English

Ruby On Rails:命名嵌套控制器和视图的正确方法是什么

[英]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输出的正确方法:

  • models/schedule.rb 型号/schedule.rb
  • models/schedule/event.rb 型号/时间表/event.rb
  • controllers/schedule/events_controllers.rb 控制器/时间表/events_controllers.rb
  • views/schedule/events/ 观看次数/时间表/事件/

or rails d scaffold schedules/event rails d scaffold schedules/event

  • models/schedules.rb models / schedules.rb
  • models/schedules/event.rb 型号/时间表/event.rb
  • controllers/schedules/events_controllers.rb 控制器/时间表/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:) 谢谢,如果您能给我一些参考,以及为什么在这两个之间选择一个答案,那将是非常有益的:)

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. 通常,脚手架时始终使用单数形式。

This might give you some insights as well 这也可能给您一些见解

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Rails 上的 Ruby 中以正确的方式将参数从视图传递到 controller - How to pass parameters in correct way from view to controller in Ruby on Rails 在Rails控制器中引用助手的正确方法是什么? - What's the correct way to reference a helper in a rails controller? 正确的Rails在另一个控制器中使用另一个控制器的动作的方式-没有嵌套属性的子窗体 - Correct Rails way of using another controller's actions within another controller - subform without nested attributes 如何在 Ruby on Rails 中创建嵌套到另一个视图文件的视图控制器? - How to create a view's controller that nested to another view file in Ruby on Rails? Ruby on Rails的正确方法是将大量数据从视图传递到控制器 - Ruby on Rails correct way to pass large amounts of data from view to controller Ruby on Rails命名约定 - Ruby on Rails naming convention in view 在rails中向控制器添加类的正确方法是什么? - what is the correct way to add classes to a controller in rails? 使用accepts_nested_attributes_for和用于在Rails中更新记录的构建方法的正确方法是什么? - What's the correct way of using the accepts_nested_attributes_for and the build methods for updating record in Rails? 在Ruby on Rails中,在基本控制器类中获得子类名称的好方法是什么? - In Ruby on Rails, what is a good way to get subclass's name in a base controller class? 在Rails中创建嵌套视图的正确方法是什么 - What is the right way of creating a nested view in rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM