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

見る:

<% @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 %>

ご覧になりたい場合は、ここに私のファイルにリンクを含めました。 マイファイルを表示するには、ここをクリックしてください。 また、すべてが予定コントローラーで行われます。この問題は、学生の表示ビュー(削除ボタンを押す場所)にあります。

解決策:わかりました。皆さんの助けを借りて、機能しました。@appointment = Appointment.find_by_id_and_student_id(params [:id]、session [:student] .user_id)@ appointment.update_attribute(:student_id、nil)

4

1 に答える 1

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

これにより、そのエラーを受信できなくなり、根本的な原因を追跡するのに役立ちます。

于 2012-03-06T16:19:00.013 に答える