18

I have a model named ActiveDns. When I run

rails g scaffold_controller ActiveDns

I get the message

Plural version of the model detected, using singularized version. Override with --force-plural.

Now, the controller and views are generated pretending that the singular is ActiveDn and the plural is ActiveDns, and I get silly stuff like link_to new_dn_path. The --force-plural argument doesn't appear to fix this:

rails g scaffold_controller ActiveDns --force-plural

still results in controllers using variables named @active_dn and views using new_dn_path, with rails 3.2.3. I am removing files between tries using rails d scaffold_controller ActiveDns.

What's the right way to do this?

4

3 回答 3

17

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

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

config/initializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable "ActiveDns"
end

然后你得到:

$ 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

这是你想要的吗?

于 2012-08-13T06:44:50.763 回答
15

我用 rails-3.2 测试过(我想它应该适用于 rails-3.x)

打开您的config/initializers/inflections.rb并添加一条规则:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'dns', 'dnses'
end

并生成控制器

rails g scaffold_controller ActiveDns

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

resources :active_dnses

然后你应该看到:

$ 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
于 2012-08-13T06:58:36.460 回答
0

我想分享一下我是如何解决我的问题的。

我有一个名为Pacmanunder的 MVC Data_Warehouse

Pacman专有名词在哪里。(不能Pacmen与 an 一起e,但应该Pacmans与 an 一起s

并且Data_Warehouse就像一个平台名称。(可以是任何东西,在这种情况下是它DataWarehouse::Pacman

所以我有

型号名称 -models/data_warehouse/pacman.rb

视图名称 -views/data_warehouse/pacmans/index.slim

控制器名称 -controller/data_warehouse/pacmans_controller.rb

问题是,Rails 读取了这个路径

data_warehouse_pacmans_path

作为

data_warehouse_pacmen_path

因为多元化。

所以

我通过添加解决了这个问题

ActiveSupport::Inflector.inflections do |inflect|
   inflect.uncountable %w( pacmans )
end

inflections.rbRails 中的文件。

希望这会有所帮助

于 2019-04-16T04:25:54.003 回答