Rails - 型号名称以S结尾

15 ruby-on-rails scaffolding

我有一个名为的模型ActiveDns.我跑的时候

rails g scaffold_controller ActiveDns
Run Code Online (Sandbox Code Playgroud)

我收到了消息

使用单一化版本检测到模型的多个版本.使用--force-plural覆盖.

现在,假装单数ActiveDn和复数是生成的控制器和视图ActiveDns,我得到愚蠢的东西link_to new_dn_path.该--force-plural论点似乎没有解决这个问题:

rails g scaffold_controller ActiveDns --force-plural
Run Code Online (Sandbox Code Playgroud)

仍然导致控制器使用带有rails 3.2.3的变量named @active_dn和views using new_dn_path.我正在尝试使用之间删除文件rails d scaffold_controller ActiveDns.

这样做的正确方法是什么?

nur*_*tin 15

这样做的正确方法是什么?

我使用变形记录不可数的实体.

配置/初始化/是inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable "ActiveDns"
end
Run Code Online (Sandbox Code Playgroud)

然后你得到:

$ rails g scaffold_controller ActiveDns
      create  app/controllers/active_dns_controller.rb
      invoke  erb
      create    app/views/active_dns
      create    app/views/active_dns/index.html.erb
      create    app/views/active_dns/edit.html.erb
      create    app/views/active_dns/show.html.erb
      create    app/views/active_dns/new.html.erb
      create    app/views/active_dns/_form.html.erb
      invoke  test_unit
      create    test/functional/active_dns_controller_test.rb
      invoke  helper
      create    app/helpers/active_dns_helper.rb
      invoke    test_unit
      create      test/unit/helpers/active_dns_helper_test.rb
Run Code Online (Sandbox Code Playgroud)

这是你想要的吗?


Jac*_*Dam 12

我用rails-3.2进行了测试(我猜它应该与rails-3.x配合使用)

打开config/initializers/inflections.rb并添加规则:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'dns', 'dnses'
end
Run Code Online (Sandbox Code Playgroud)

并生成控制器

rails g scaffold_controller ActiveDns
Run Code Online (Sandbox Code Playgroud)

并添加路线到您的config/routes.rb文件

resources :active_dnses
Run Code Online (Sandbox Code Playgroud)

然后你应该看到:

$ rake routes

   active_dnses GET    /active_dnses(.:format)          active_dnses#index
                POST   /active_dnses(.:format)          active_dnses#create
 new_active_dns GET    /active_dnses/new(.:format)      active_dnses#new
edit_active_dns GET    /active_dnses/:id/edit(.:format) active_dnses#edit
     active_dns GET    /active_dnses/:id(.:format)      active_dnses#show
                PUT    /active_dnses/:id(.:format)      active_dnses#update
                DELETE /active_dnses/:id(.:format)      active_dnses#destroy
Run Code Online (Sandbox Code Playgroud)