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



我正在遵循 Rails 入门指南,并在提示以下代码时将其提高到 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

欢迎来到 SO 和 Ruby on Rails 的奇妙世界:D

看起来你需要移动

resources :posts do
  resources :comments
end

进入 routes.draw 块

例如/

Blog::Application.routes.draw do     
  resources :posts
  root to: "welcome#index"
  resources :posts do
    resources :comments
  end
end

最新更新