Rails首字母缩略词变形分裂中间首字母缩略词

时间:2014-09-19 17:30:54

标签: ruby-on-rails ruby-on-rails-4

我正在尝试将脚手架生成器用于名为“RSVP”的模型和控制器。我希望定义变形足以使它创建一个带有RSVPController类的rsvps_controller.rb和一个带有RSVP表的RSVPs模型。我也发现RsvpController和Rsvp可以接受。

不幸的是,似乎已将'RSVP'拆分为'RSV P',因此rails generate scaffold RSVP ...创建了一系列不同的拼写,包括rsv_ps_controller,RsvPsController和rSVP。唯一可接受的结果是名为rsvp.rb的模型文件,其中包含一个Rsvp类。

有没有正确的方法来达到预期的效果?我正在使用Rails 4.1.4

我的config / initializers / inflections.rb看起来像:

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym 'RSVP'
  inflect.irregular 'RSVP', 'RSVPs'
end

我尝试过这两种的所有组合(即,既可以单独也可以同时定义)。两者似乎都得到了相同的结果。

> bundle exec rails generate scaffold RSVP account:references ...
      invoke  active_record
      create    db/migrate/20140918213359_create_rSVPs.rb
      create    app/models/rsvp.rb
      invoke    test_unit
      create      test/models/rsvp_test.rb
      create      test/fixtures/rSVPs.yml
      invoke  resource_route
       route    resources :rSVPs
      invoke  scaffold_controller
      create    app/controllers/rsv_ps_controller.rb
      invoke    erb
      create      app/views/rsv_ps
      create      app/views/rsv_ps/index.html.erb
      create      app/views/rsv_ps/edit.html.erb
      create      app/views/rsv_ps/show.html.erb
      create      app/views/rsv_ps/new.html.erb
      create      app/views/rsv_ps/_form.html.erb
      invoke    test_unit
      create      test/controllers/rsv_ps_controller_test.rb
      invoke    helper
      create      app/helpers/rsv_ps_helper.rb
      invoke      test_unit
      create        test/helpers/rsv_ps_helper_test.rb
      invoke    jbuilder
      create      app/views/rsv_ps/index.json.jbuilder
      create      app/views/rsv_ps/show.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/rsv_ps.js.coffee
      invoke    scss
      create      app/assets/stylesheets/rsv_ps.css.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.css.scss

1 个答案:

答案 0 :(得分:0)

ActiveSupport::Inflector::Inflections.acronym方法的文档表明您应该为单数和复数形式添加首字母缩略词条目。

这意味着以下内容应该有效。但是,这对我来说似乎仍然无效。

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym 'RSVP'
  inflect.acronym 'RSVPs'
end

来自doc:

  # Note: Acronyms that are passed to +pluralize+ will no longer be
  # recognized, since the acronym will not occur as a delimited unit in the
  # pluralized result. To work around this, you must specify the pluralized
  # form as an acronym as well:
  #
  #    acronym 'API'
  #    camelize(pluralize('api')) # => 'Apis'
  #
  #    acronym 'APIs'
  #    camelize(pluralize('api')) # => 'APIs'