获取NoMethodError:ROR中的未定义方法错误

时间:2016-06-07 13:46:25

标签: ruby ruby-on-rails-4

使用ROR运行命令rake routes时出现以下错误。

  

错误:

NoMethodError: undefined method `resources' for nil:NilClass
C:/Sites/bookshelf/config/routes.rb:4:in `block (2 levels) in <top (required)>'
C:/Sites/bookshelf/config/routes.rb:3:in `block in <top (required)>'
C:/Sites/bookshelf/config/routes.rb:1:in `<top (required)>'
C:in `execute_if_updated'
C:/Sites/bookshelf/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => routes => environment
(See full trace by running task with --trace)

我正在解释下面的代码。

Rails.application.routes.draw do 
       root 'books#index'
       resources :books  do |book|
           book.resources :comments, :only => :create
       end
end

请帮我解决此错误。

1 个答案:

答案 0 :(得分:2)

Rails.application.routes.draw do 
       root 'books#index'
       resources :books  do
           resources :comments, :only => :create
       end
end