RAILS中的NoMethodError(未定义的方法`fetch_value'为nil:NilClass)

时间:2016-10-05 03:49:31

标签: ruby-on-rails ruby ruby-on-rails-4 activerecord

SQL中的表名:road_ratings

路线:

get '/roadRatings', to: "road_ratings#index"

控制器:road_ratings_controller.rb

class RoadRatingsController < ApplicationController

    def index
        @data = RoadRating.all
        render json: @data
    end

end

型号:road_rating.rb

class RoadRating < ActiveRecord::Base
end

运行上述API后,我收到rails服务器日志中的错误

NoMethodError (undefined method `fetch_value' for nil:NilClass):

在浏览器上出现以下错误

NoMethodError in RoadRatingsController#index

但是,当我在SQL上的rails日志中生成查询SELECT road_ratings .* FROM road_ratings LIMIT 1时,我得到了正确的结果

Rails日志:

Started GET "/roadRatings" for ::1 at 2016-10-04 23:55:57 -0400
Processing by RoadRatingsController#index as HTML
  RoadRating Load (0.4ms)  SELECT  `road_ratings`.* FROM `road_ratings` LIMIT 1
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.4ms)

NoMethodError (undefined method `fetch_value' for nil:NilClass):
  app/controllers/road_ratings_controller.rb:4:in `index'


  Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_source.erb (5.6ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.4ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.1ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (23.1ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (0.8ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.3ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.4ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (16.7ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (0.3ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.7ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (39.4ms)

Rake Routes:

Kartiks-MacBook-Air:Mayor_project kartik$ rake routes
     Prefix Verb URI Pattern            Controller#Action
            GET  /                      home#default
    streets GET  /streets(.:format)     streets#index
            GET  /street/:id(.:format)  streets#show
   potholes GET  /potholes(.:format)    potholes#index
roadRatings GET  /roadRatings(.:format) road_ratings#index

0 个答案:

没有答案