2012-06-26 32 views
0

這是工作前幾天,不知道發生了什麼地方出了錯......NoMethodError在Human_resources /葉#指數

**undefined method `name' for nil:NilClass** 

Extracted source (around line #26): 

23:    %td= number_with_precision(employee.compensation_leave_balance, precision:1) 
24: #calendar.tab-pane.fade 
25:  = calendar(:year => 2012, :month => 6, :first_day_of_week => 1, summary: "Leave Calendar", calendar_title: "June", month_header: true) do |date| 
26:  - render_leave_calendar_cell(date) 
27: #trash.tab-pane.fade 
28:  = render 'table', leaves: @leaves.where(deleted: true) 

app/helpers/leaves_helper.rb:11:in `block in events_for' 
app/helpers/leaves_helper.rb:10:in `events_for' 
app/helpers/leaves_helper.rb:4:in `render_leave_calendar_cell' 
app/views/human_resources/leaves/index.html.haml:26:in `block in _app_views_human_resources_leaves_index_html_haml__145883348_88978910' 
app/helpers/calendar_helper.rb:146:in `call' 
app/helpers/calendar_helper.rb:146:in `block in calendar' 
app/helpers/calendar_helper.rb:145:in `upto' 
app/helpers/calendar_helper.rb:145:in `calendar' 
app/views/human_resources/leaves/index.html.haml:25:in `_app_views_human_resources_leaves_index_html_haml__145883348_88978910' 

真的不知道自己錯在哪裏

a/h/leaves_helper.rb                               
    1 module LeavesHelper 
    2 def render_leave_calendar_cell(date) 
    3  html = content_tag(:span, date.day, class: 'dayDisplay') 
    4  html += content_tag(:div, events_for(date)) 
    5  raw(html) 
    6 end 
    7 
    8 def events_for(date)                             
    9  html = "" 
    10  current_company.leaves.where("start_date <= '#{date}' and return_date > '#{date}'").where(deleted: false).each do |leave| 
    11  html += content_tag(:div, leave.applicant.name, class: 'leaveName') 
    12  end 
    13  raw html 
    14 end 

它可能是日期零嗎?如何解決這個問題> <

感激 比利

+0

錯誤發生在leaves_helper的第11行,這意味着您的查詢的葉子'current_company.leaves.where(「start_date <='#{date}'和return_date>'#{date}'」)。where(deleted :false)''正在返回有一個'leave'文件,因爲你的'申請者'不存在,因此'leave.applicant'返回nil – abhas

+0

我知道,但是我該怎麼辦才能解決這個問題? – FCW

回答

0

爲abhas已經說過,leave.applicant是葉子中的至少一個零。

去你的數據庫,找出它是哪個。然後找出如何處理你的葉子。刪除它們,重新添加失蹤的申請人或者任何數據遷移可能是明智的。

速戰速決將跳過如果申請人丟失:

html += content_tag(:div, leave.applicant.name, class: 'leaveName') if leave.applicant.present? 

我也看看,如果你正確地在你的應用程序配置的刪除級聯。這經常導致這樣的問題。如果您想在這方面強制執行安全性,則應添加數據庫約束,以確保沒有引用的實體被刪除。

+0

非常感謝Phoet,它確實解決了我的問題! – FCW