在其中添加额外代码后,删除操作不起作用
delete action is not working after adding extra code inside it
我的控制器中有删除操作,这是 pickups_controller.rb
中的代码
def delete
@pickup = Pickup.find(params[:id])
if !@pickup.nil?
@pickup.destroy
render json: { success_message: "Success!, Pickup is deleted." }, status: :ok
end
end
我使用 javascript json 通过使用 assets/javascripts/pickups.js
按下按钮来调用删除操作
document.addEventListener("DOMContentLoaded", function(event) {
var xhttp = new XMLHttpRequest();
// delete the pickup you choose
$('.removepickup.btn.btn-primary').on('click', function() {
var pickup_div = $(this).parents('.removepickupparent');
var pickup_id = pickup_div.attr('id');
var x = "../deletepickup?id=" + pickup_id;
$.ajax({
type: "POST",
url: x,
success: function(data) {
var success = data.success_message;
$(".successr"+ pickup_id).text(success).show(0).delay(1000).hide(0);
setTimeout(function () {
location.reload();
}, 1000);
},
error: function (xhr, ajaxOptions, thrownError){
if(xhr.status==404) {
$(".errorl"+ pickup_id).text("Fail!, pickup Is Already Deleted Before").show(0).delay(1000).hide(0);
setTimeout(function () {
location.reload();
}, 2000);
}
}
});
});
// when pressing on this button, it redirects you to create pickup page
$('.addpickup.btn.btn-primary').on('click', function() {
var success = "Redirecting to add pickup Page"
$(".successp").text(success).show(0).delay(2000).hide(0);
setTimeout(function () {
$(location).attr('href', '../createpickup');
}, 2000);
});
});
该功能运行良好,但在删除操作中添加 4 行额外代码时,它不起作用,这是在我的删除操作中添加 4 行额外代码后的代码,该操作不起作用.
def delete
@pickup = Pickup.find(params[:id])
if !@pickup.nil?
# the start of the extra code
@trip = Trip.find(@pickup.trip_id)
if !@trip.nil?
@trip.seatsno = @trip.seatsno + 1
@trip.save
end
# the end of the extra code
@pickup.destroy
render json: { success_message: "Success!, Pickup is deleted." }, status: :ok
end
end
有什么解决办法吗? .. 知道我在 Rails
的 Ruby 仍然是初学者
注:
我使用了 byebug,当到达 etra 代码的第一行时,我在本地服务器终端出现了这个错误
"request.env["action_dispatch.show_detailed_exceptions"] ||= show_detailed_exceptions?"
与实际答案相比,此答案更像是重构建议,但它也可能会解决您的问题。
您可以将您的操作重构为:
def delete
@pickup = Pickup.find(params[:id])
# no need to test @pickup.nil? here because `find` method raise
# an ActiveRecord::RecordNotFound error if the record is not found
# which should be caught by ApplicationController to render a 404
if @pickup.destroy
@pickup.trip.update_attributes(seatsno: @pickup.trip.seatsno + 1)
render json: { success_message: "Success!, Pickup is deleted." }, status: :ok
else
render json: { error_message: "Error, Pickup could not be deleted." }, status: 409
end
end
更好的是,将递增 seatsno
的关注转移到 Pickup
模型:
# app/models/pickup.rb
after_destroy :increment_trip_seatsno
def increment_trip_seatsno
self.trip.update_attributes(seatsno: self.trip.seatsno + 1)
end
并消除 Controller 的顾虑。这样,每次通过 Rails(控制台或应用程序中的其他地方)销毁 Pickup
记录时,行程都会相应更新。
使用 find_by
而不是 find
方法。 find' method raises the exception if a particular record is not found, while
find_by` returns nil.
用法:
find_by(id: params[:id])
我的控制器中有删除操作,这是 pickups_controller.rb
中的代码 def delete
@pickup = Pickup.find(params[:id])
if !@pickup.nil?
@pickup.destroy
render json: { success_message: "Success!, Pickup is deleted." }, status: :ok
end
end
我使用 javascript json 通过使用 assets/javascripts/pickups.js
按下按钮来调用删除操作document.addEventListener("DOMContentLoaded", function(event) {
var xhttp = new XMLHttpRequest();
// delete the pickup you choose
$('.removepickup.btn.btn-primary').on('click', function() {
var pickup_div = $(this).parents('.removepickupparent');
var pickup_id = pickup_div.attr('id');
var x = "../deletepickup?id=" + pickup_id;
$.ajax({
type: "POST",
url: x,
success: function(data) {
var success = data.success_message;
$(".successr"+ pickup_id).text(success).show(0).delay(1000).hide(0);
setTimeout(function () {
location.reload();
}, 1000);
},
error: function (xhr, ajaxOptions, thrownError){
if(xhr.status==404) {
$(".errorl"+ pickup_id).text("Fail!, pickup Is Already Deleted Before").show(0).delay(1000).hide(0);
setTimeout(function () {
location.reload();
}, 2000);
}
}
});
});
// when pressing on this button, it redirects you to create pickup page
$('.addpickup.btn.btn-primary').on('click', function() {
var success = "Redirecting to add pickup Page"
$(".successp").text(success).show(0).delay(2000).hide(0);
setTimeout(function () {
$(location).attr('href', '../createpickup');
}, 2000);
});
});
该功能运行良好,但在删除操作中添加 4 行额外代码时,它不起作用,这是在我的删除操作中添加 4 行额外代码后的代码,该操作不起作用.
def delete
@pickup = Pickup.find(params[:id])
if !@pickup.nil?
# the start of the extra code
@trip = Trip.find(@pickup.trip_id)
if !@trip.nil?
@trip.seatsno = @trip.seatsno + 1
@trip.save
end
# the end of the extra code
@pickup.destroy
render json: { success_message: "Success!, Pickup is deleted." }, status: :ok
end
end
有什么解决办法吗? .. 知道我在 Rails
的 Ruby 仍然是初学者注:
我使用了 byebug,当到达 etra 代码的第一行时,我在本地服务器终端出现了这个错误
"request.env["action_dispatch.show_detailed_exceptions"] ||= show_detailed_exceptions?"
与实际答案相比,此答案更像是重构建议,但它也可能会解决您的问题。
您可以将您的操作重构为:
def delete
@pickup = Pickup.find(params[:id])
# no need to test @pickup.nil? here because `find` method raise
# an ActiveRecord::RecordNotFound error if the record is not found
# which should be caught by ApplicationController to render a 404
if @pickup.destroy
@pickup.trip.update_attributes(seatsno: @pickup.trip.seatsno + 1)
render json: { success_message: "Success!, Pickup is deleted." }, status: :ok
else
render json: { error_message: "Error, Pickup could not be deleted." }, status: 409
end
end
更好的是,将递增 seatsno
的关注转移到 Pickup
模型:
# app/models/pickup.rb
after_destroy :increment_trip_seatsno
def increment_trip_seatsno
self.trip.update_attributes(seatsno: self.trip.seatsno + 1)
end
并消除 Controller 的顾虑。这样,每次通过 Rails(控制台或应用程序中的其他地方)销毁 Pickup
记录时,行程都会相应更新。
使用 find_by
而不是 find
方法。 find' method raises the exception if a particular record is not found, while
find_by` returns nil.
用法:
find_by(id: params[:id])