2012-03-06 50 views
0

好吧,我正在爲教授和學生創建一個約會網站來登錄和創建/編輯約會。我已經完成了教授方面的工作,但我正在與學生們奮鬥。現在我正在努力讓學生點擊他們預約旁邊的按鈕,從他們與教授之間的約會中解脫出來。現在我可以輕鬆地刪除約會ID,約會將會消失,但是我想從約會中刪除student_id,以便其他學生稍後可以選擇該約會。這裏是我的代碼: 控制器:你沒有想到它會有一個零對象(約會頁面)

def destroy 
if session[:professor] != nil 

    @appointment = Appointment.find(params[:id]) 
    @appointment.destroy 
end 
if session[:student] != nil 
@appointment = Appointment.find_by_id_and_student_id(params[:id],params[:student_id]) 
@appointment.destroy(params[:student_id]) 
end 
end 

VIEW:

<% @appointments.each do |appointment| %> 
<tr> 
<td><%= appointment.professor_id %></td> 
<td><%= appointment.student_id %></td> 
<td><%= appointment.timeslot %></td> 
<td><%= link_to 'remove appointment', appointment, confirm: 'Are you sure?', method: 
:delete %></td> 
</tr> 
<% end %> 

,如果你想看看我在這裏跟我的文件包含的鏈接。 CLICK HERE TO VIEW MY FILES. 此外,一切都發生在約會控制器。並且這個問題出現在顯示學生視圖中(你按下刪除按鈕)。

SOLUTION: 好吧,所以我得到它的工作,由於我從你們的幫助。因此,這裏是我做過什麼: @appointment = Appointment.find_by_id_and_student_id(PARAMS [:編號],會話[:學生] .user_id) @ appointment.update_attribute(:student_id數據,無)

+0

哪些行發生錯誤? – 2012-03-06 15:57:05

+0

app/controllers/appointmentments_controller.rb:118:在'destroy' – user1179269 2012-03-06 16:00:38

+0

這並不能幫助您知道您提供的代碼中的哪一行,因爲它沒有任何對行號的引用。在指出該行的代碼中添加註釋。 – 2012-03-06 16:01:47

回答

0

幾乎可以肯定在這裏的錯誤是Appointment.find_by_id_and_student_id返回nil。重構代碼是這樣的:

@appointment = Appointment.find_by_id_and_student_id(params[:id],params[:student_id]) 
if @appointment 
    @appointment.destroy(params[:student_id]) 
else 
    flash[:error] = 'An appointment could not be found for that student.' 
end 

這會阻止您接收錯誤,並幫助你追查其根源。

+0

錯誤是在行:@ appointment.destroy(params [:student_id]) – user1179269 2012-03-06 16:26:46

+1

是的,但可以推測錯誤是'''@預約'''是零。所以你需要糾正你分配'''@約會'''的行。如果不是,那麼你需要爲你的錯誤發佈回溯。 – Veraticus 2012-03-06 16:28:09

+0

另外,如果您必須將參數傳遞給'destroy',則意味着'約會'不是單個模型對象,而是一個集合。 – 2012-03-06 16:32:12

相關問題