简体   繁体   English

Rails 3 拐点问题

[英]Rails 3 Inflection Problem

I'm having a problem with generating scaffold for a Regatta.我在为 Regatta 生成脚手架时遇到问题。 When I run当我跑

rails g scaffold Regatta name:string start_date:datetime

I get a model named regattum and a controller called regatta_controller (instead of regattas_controller)我得到一个名为 regattum 的 model 和一个名为 regatta_controller 的 controller(而不是 regattas_controller)

  invoke  active_record
  create    db/migrate/20110609221608_create_regatta.rb
  create    app/models/regattum.rb
  invoke    test_unit
  create      test/unit/regattum_test.rb
  create      test/fixtures/regatta.yml
   route  resources :regatta
  invoke  scaffold_controller
  create    app/controllers/regatta_controller.rb
  invoke    erb
  create      app/views/regatta
  create      app/views/regatta/index.html.erb
  create      app/views/regatta/edit.html.erb
  create      app/views/regatta/show.html.erb
  create      app/views/regatta/new.html.erb
  create      app/views/regatta/_form.html.erb
  invoke    test_unit
  create      test/functional/regatta_controller_test.rb
  invoke    helper
  create      app/helpers/regatta_helper.rb
  invoke      test_unit
  create        test/unit/helpers/regatta_helper_test.rb
  invoke  stylesheets

identical public/stylesheets/scaffold.css相同的公共/样式表/脚手架.css

Obviously this is an inflection problem, but whenever I modify /config/initializers/inflections.rb I get an error saying:显然这是一个拐点问题,但是每当我修改 /config/initializers/inflections.rb 时,我都会收到一条错误消息:

The name 'Regatta' is either already used in your application or reserved by Ruby on Rails. Please choose an alternative and run this generator again.

I have tried everything I can think of to make it work, but I keep getting an error.我已经尝试了所有我能想到的让它工作,但我一直收到错误。 Any advice on a solution or a workaround would be greatly appreciated!任何有关解决方案或解决方法的建议将不胜感激!


Update更新

Here are some of the things I have tried:以下是我尝试过的一些事情:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'regatta', 'regattas'
end

That didn't change anything so I tried a few of the options below, among others,in varying combinations to no avail:这并没有改变任何东西,所以我尝试了以下一些选项,其中包括不同的组合,但无济于事:

 inflect.plural 'regatta', 'regattas'
  inflect.singular 'regattas', 'regatta'
  inflect.singular 'regatta', 'regatta'

Update 2更新 2

Here is the code I used in inflections.rb once I figured out what I was doing wrong:这是我在发现自己做错了什么后在 inflections.rb 中使用的代码:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.plural 'regatta', 'regattas'
  inflect.singular 'regatta', 'regatta'
  inflect.singular 'regattas', 'regatta'
end

Hopefully this will help someone out in the future!希望这将有助于将来的人!

I recognize this post is old, but I thought I would contribute my findings as well.我承认这篇文章很旧,但我想我也会贡献我的发现。

I have been integrating rpsec and cucumber into my application after much development (yes; naughty, naughty).经过大量开发后,我一直在将 rpsec 和 cucumber 集成到我的应用程序中(是的;淘气,淘气)。 I wanted to generate the rspec stubs for my models and get my application unit tested.我想为我的模型生成 rspec 存根并测试我的应用程序单元。 I am usually able to move an associated migration out of db/migrate/ and run 'bundle exec rails g ' to regenerate the scaffolds and in this case, the rspec stubs.我通常能够将关联的迁移移出 db/migrate/ 并运行“bundle exec rails g”来重新生成脚手架,在这种情况下,就是 rspec 存根。 Not this time.这次不行。

<terminal> <终端>

bundle exec rails g scaffold User email:string ... 
      updated_at:datetime roles_mask:integer --trace

      invoke  active_record

The name 'User' is either already used in your application 
      or reserved by Ruby on Rails. Please choose an alternative 
      and run this generator again.

My problem was resolved by temporarily commenting out the 'devise_for' line in my 'config/routes.rb' file通过暂时注释掉我的“config/routes.rb”文件中的“devise_for”行解决了我的问题

config/routes.rb配置/路由.rb

#devise_for :users

Bingo.答对了。 And this isn't the only time that Devise has surprised me with 'black magic' that is not readily apparent.而这并不是 Devise 唯一一次让我惊讶于不那么明显的“黑魔法”。 I un-commented the line once my rspec stubs were generated, of course.当然,一旦生成了我的 rspec 存根,我就取消了该行的注释。 Now it's off to writing my unit tests!现在开始编写我的单元测试了!

Given the error message saying that Regatta is already in use in your application (it's obviously not reserved by Rails), I'm guessing the Regattum model is still in place.鉴于错误消息表明Regatta已在您的应用程序中使用(它显然不是 Rails 保留的),我猜Regattum model 仍然存在。

I'd recommend destroying the scaffold and trying again.我建议破坏脚手架并重试。 It sounds like, once a word is already defined in the application, Rails has a built-in protection against changing the inflection.听起来,一旦应用程序中已经定义了一个词,Rails 就有一个内置的保护机制来防止改变变形。 So, this error being thrown is to protect against unexpected behavior in this very situation.因此,抛出这个错误是为了防止在这种情况下出现意外行为。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM