Rails 4:使用现有模型生成脚手架控制器,它仅在重新加载页面两次后才能工作

时间:2016-01-10 14:32:03

标签: ruby-on-rails

我先生成模型

  

rails g model cat name

然后我使用型号名称

制作脚手架控制器
  

rails g scaffold_controller zoo / cat name --model-name = zoo / cat

然后我添加路线

  

命名空间:动物园做什么   资源:猫
  端

然后我编辑模型

  

类Zoo :: Cat<的ActiveRecord :: Base的
  端

有趣的是它最初失败了,错误

  

Zoo :: CatsController#index中的LoadError   无法自动加载常量Cat,预期/home/minh/Desktop/fnl110/app/models/cat.rb来定义它     def指数
      @zoo_cats = Zoo :: Cat.all
    结束

然后我重新加载页面,它没有任何错误。这是服务员说的:

Started GET "/zoo/cats" for 127.0.0.1 at 2016-01-10 21:05:30 +0700
  ActiveRecord::SchemaMigration Load (0.1ms)  SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by Zoo::CatsController#index as HTML
Completed 500 Internal Server Error in 14ms (ActiveRecord: 0.0ms)

LoadError (Unable to autoload constant Cat, expected /home/minh/Desktop/fnl110/app/models/cat.rb to define it):
  app/controllers/zoo/cats_controller.rb:7:in `index'


  Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_source.erb (7.1ms)
  Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.2ms)
  Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms)
  Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (29.2ms)
  Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.5ms)
  Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.4ms)
  Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms)
  Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.4ms)
  Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (19.6ms)
  Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.5ms)
  Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.6ms)
  Rendered /home/minh/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (42.4ms)


Started GET "/zoo/cats" for 127.0.0.1 at 2016-01-10 21:10:15 +0700
Processing by Zoo::CatsController#index as HTML
  Zoo::Cat Load (0.2ms)  SELECT `cats`.* FROM `cats`
  Rendered zoo/cats/index.html.erb within layouts/application (8.6ms)
Completed 200 OK in 200ms (Views: 197.5ms | ActiveRecord: 1.2ms)


Started GET "/assets/animal/bears.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700


Started GET "/assets/animal/cats.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700


Started GET "/assets/animal/dogs.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700


Started GET "/assets/scaffolds.self-d5c911709e0018272624ff2abf5748b5d0ff72c54f791bb9bcefd817c3322285.css?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700


Started GET "/assets/turbolinks.self-c37727e9bd6b2735da5c311aa83fead54ed0be6cc8bd9a65309e9c5abe2cbfff.js?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700


Started GET "/assets/jquery_ujs.self-d456baa54c1fa6be2ec3711f0a72ddf7a5b2f34a6b4f515f33767d6207b7d4b3.js?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700


Started GET "/assets/application.self-e80e8f2318043e8af94dddc2adad5a4f09739a8ebb323b3ab31cd71d45fd9113.css?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700


Started GET "/assets/animal/bears.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700


Started GET "/assets/jquery.self-a714331225dda820228db323939889f149aec0127aeb06255646b616ba1ca419.js?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700


Started GET "/assets/animal/dogs.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700


Started GET "/assets/application.self-3b8dabdc891efe46b9a144b400ad69e37d7e5876bdc39dee783419a69d7ca819.js?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700


Started GET "/assets/animal/cats.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" for 127.0.0.1 at 2016-01-10 21:10:16 +0700

P / S:

的原因
  

rails g scaffold_controller zoo / cat name --model-name = zoo / cat

  

rails g scaffold_controller zoo / cat --model-name = zoo / cat
  =>控制器不采用模型属性

  

rails g scaffold_controller zoo / cat --model-name = cat
  =>错误:未定义的局部变量或方法`new_cat_path'

1 个答案:

答案 0 :(得分:0)

错误是正常的,您的模型应该在动物园目录中移动,因此app/models/zoo/cat.rb(而不是app/models/cat.rb)。如果你这样做,它会立即起作用。

我可能错了,但我相信会发生以下情况:

  1. 当您第一次加载页面时,rails会懒惰地加载模型,并且会因为请求而给您错误,但是因为文件app/models/cat.rb应该定义Cat而不是{ {1}};
  2. 即使出现错误,rails也会加载Zoo::Cat模型,因此当您重新加载页面时,它不会遇到错误,因为模型已经加载,因此请求成功。