生成控制器(Rails入门,第6.4章)

时间:2013-10-31 11:40:31

标签: ruby-on-rails

我正在关注Getting Started with Rails Guide并在提示此代码时将其提升至6.4:

$ rails generate controller Comments

我的终端说出来了:

johns-MacBook-Pro:blog john$ rails g controller Comments
/Users/john/.rvm/gems/ruby-2.0.0-p247@global/gems/railties-4.0.0/lib/rails/application/routes_reloader.rb:10:in `rescue in execute_if_updated': Rails::Application::RoutesReloader#execute_if_updated delegated to updater.execute_if_updated, but updater is nil: #<Rails::Application::RoutesReloader:0x007faa4156ec60 @paths=["/Users/john/rails/blog/config/routes.rb"], @route_sets=[#<ActionDispatch::Routing::RouteSet:0x007faa41933a30>]> (RuntimeError)
    from /Users/john/.rvm/gems/ruby-2.0.0-p247@global/gems/railties-4.0.0/lib/rails/application/routes_reloader.rb:6:in `execute_if_updated'
    from /Users/john/.rvm/gems/ruby-2.0.0-p247@global/gems/railties-4.0.0/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
    from /Users/john/.rvm/gems/ruby-2.0.0-p247@global/gems/railties-4.0.0/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/john/.rvm/gems/ruby-2.0.0-p247@global/gems/railties-4.0.0/lib/rails/initializable.rb:30:in `run'
    from /Users/john/.rvm/gems/ruby-2.0.0-p247@global/gems/railties-4.0.0/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /Users/john/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:150:in `block in tsort_each'
    from /Users/john/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:183:in `block (2 levels) in each_strongly_connected_component'
    from /Users/john/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:219:in `each_strongly_connected_component_from'
    from /Users/john/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:182:in `block in each_strongly_connected_component'
    from /Users/john/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:180:in `each'
    from /Users/john/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:180:in `each_strongly_connected_component'
    from /Users/john/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:148:in `tsort_each'
    from /Users/john/.rvm/gems/ruby-2.0.0-p247@global/gems/railties-4.0.0/lib/rails/initializable.rb:54:in `run_initializers'
    from /Users/john/.rvm/gems/ruby-2.0.0-p247@global/gems/railties-4.0.0/lib/rails/application.rb:215:in `initialize!'
    from /Users/john/.rvm/gems/ruby-2.0.0-p247@global/gems/railties-4.0.0/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /Users/john/rails/blog/config/environment.rb:5:in `<top (required)>'
    from /Users/john/.rvm/gems/ruby-2.0.0-p247@global/gems/railties-4.0.0/lib/rails/application.rb:189:in `require'
    from /Users/john/.rvm/gems/ruby-2.0.0-p247@global/gems/railties-4.0.0/lib/rails/application.rb:189:in `require_environment!'
    from /Users/john/.rvm/gems/ruby-2.0.0-p247@global/gems/railties-4.0.0/lib/rails/commands.rb:45:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'
johns-MacBook-Pro:blog john$ 

我是编码新手,不知道从哪里开始寻找解决方案。

这是routes.rb:

Blog::Application.routes.draw do     
  resources :posts

  root to: "welcome#index"
end

resources :posts do
  resources :comments
end

1 个答案:

答案 0 :(得分:2)

欢迎来到SO和Ruby on Rails的精彩世界:D

看起来你需要移动

resources :posts do
  resources :comments
end

进入routes.draw块

e.g ./

Blog::Application.routes.draw do     
  resources :posts

  root to: "welcome#index"

  resources :posts do
    resources :comments
  end
end