我想訪問我的dirts /索引頁面,但我得到下面的圖片中顯示此錯誤。 零:NilClass當ByeBug報告預期結果
所以我把byebug在#total_trip_miles看看是怎麼回事:
class Driver < ApplicationRecord
has_many :dirts
validates :name, presence: true, uniqueness: true
def total_trip_miles
byebug
collect_trip_miles.round.to_s
end
def average_speed
total_distance = collect_trip_miles
total_rate = collect_trip_seconds
((total_distance/total_rate)*3600).round
end
def collect_trip_miles
dirts.map do |trip|
trip.distance.to_f
end.inject(:+)
end
def collect_trip_seconds
seconds = []
dirts.map do |trip|
seconds << trip.change_in_time(trip.start_time, trip.end_time)
end
seconds.inject(:+)
end
end
當我打電話collect_trip_miles.round.to_s
從byebug我得到"55"
,這是我想通過我的規格到底是什麼:
我在dirt/index.html.erb
調用該方法從#drivers
:
<ul id="trips">
<% drivers.each do |driver| %>
<li>
<article class="driver">
<header>
<h2><%= show_name_of(driver) %></h2>
</header>
<p>
<%= "#{show_total_miles_of(driver)} Miles" %>
</p>
<table>
</table>
<footer>
</footer>
</article>
</li>
<% end %>
</ul>
而這些方法都是從我app/helpers/dirts_helper.rb
module DirtsHelper
def show_name_of(driver)
the_driver(driver).name.capitalize
end
def show_total_miles_of(driver)
the_driver(driver).total_trip_miles
end
def the_driver(driver)
Driver.find(driver.first)
end
def drivers
@driver_store = {}
@drivers.each_with_index do |driver, dirts|
@driver_store[driver.id] = driver.dirts
end
@driver_store
end
end
叫,然後控制器動作來自app/controllers/dirts_controller.rb
class DirtsController < ApplicationController
def index
@drivers = Driver.all
end
end
基於我所描述和證據顯示,有some-超級明顯 - 我應該爲自己感到羞恥的原因是爲什麼我的驅動程序模型的方法在我的本地主機中爆炸,但調試是按預期報告的?
很高興你明白了。 – dps