最开始,我用 ajax 发送了一个 delete 请求,遇到了这个问题
在 destroy action 中 redirect_to 到一个 index 页面,rails 用了 delete 请求而不是 get
google 了一下,说用 status: 303
带上 303 之后,看 server 确实是用的 get, 也不报错了,该 render 的都 render 了,可就是不刷新页面
请问这是为什么鸭
edit.js.erb
$('#confirm_button').click(function() {
var url = $('.js-submit-delete-section').data('url')
$.ajax({
method: 'delete',
url: url,
})
})
def destroy
@section.destroy
...
flash[:notice] = "删除成功"
render js: "window.location = '#{course_sections_path(@course)}'"
end
log
Started DELETE "/courses/5b582404421aa97728ee4e2f/sections/5b681976421aa9c55b549830" for 127.0.0.1 at 2018-10-25 20:49:56 +0800
Processing by Courses::SectionsController#destroy as */*
....
Started GET "/courses/5b582404421aa97728ee4e2f/sections" for 127.0.0.1 at 2018-10-25 20:49:56 +0800
Processing by Courses::SectionsController#index as HTML
controller
def destroy
@section.destroy
if params[:destroy_content] && @section.content.destroy
flash[:success] = "成功解绑并删除"
end
if params[:unbind_course_from_content] && @section.content.update(course: nil)
flash[:success] = "成功解绑并转换为直播"
end
flash[:notice] = "删除成功"
redirect_to action: :index
end
log
Started DELETE "/courses/5b582404421aa97728ee4e2f/sections/5b681970421aa9c55b54982e" for 127.0.0.1 at 2018-10-25 20:51:16 +0800
Processing by Courses::SectionsController#destroy as */*
Parameters: {"course_id"=>"5b582404421aa97728ee4e2f", "id"=>"5b681970421aa9c55b54982e"}
MONGODB | localhost:27017 | MagNet_development.find | STARTED | {"find"=>"users", "filter"=>{"_id"=>BSON::ObjectId('56a8b4c9feec620985000000')}, "limit"=>1, "
MONGODB | localhost:27017 | MagNet_development.update | SUCCEEDED | 0.0023810000000000003s
Redirected to http://localhost:3000/courses/5b582404421aa97728ee4e2f/sections
Completed 302 Found in 78ms
Started DELETE "/courses/5b582404421aa97728ee4e2f/sections" for 127.0.0.1 at 2018-10-25 20:51:16 +0800
ActionController::RoutingError (No route matches [DELETE] "/courses/5b582404421aa97728ee4e2f/sections"):
actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
.....
controller
def destroy
@section.destroy
if params[:destroy_content] && @section.content.destroy
flash[:success] = "成功解绑并删除"
end
if params[:unbind_course_from_content] && @section.content.update(course: nil)
flash[:success] = "成功解绑并转换为直播"
end
flash[:notice] = "删除成功"
redirect_to action: :index, status: 303
end
log
Started DELETE "/courses/5b582404421aa97728ee4e2f/sections/5bd1bc8aa33259a591f0a71d" for 127.0.0.1 at 2018-10-25 20:52:33 +0800
Processing by Courses::SectionsController#destroy as */*
Parameters: {"course_id"=>"5b582404421aa97728ee4e2f", "id"=>"5bd1bc8aa33259a591f0a71d"}
MONGODB | localhost:27017 | MagNet_development.find | STARTED | {"find"=>"users", "filter"=>{"_id"=>BSON::ObjectId('56a8b4c9feec620985000000')}, "limit"=>1,
MONGODB | localhost:27017 | MagNet_development.update | SUCCEEDED | 0.0015010000000000002s
Redirected to http://localhost:3000/courses/5b582404421aa97728ee4e2f/sections
Completed 303 See Other in 45ms
Started GET "/courses/5b582404421aa97728ee4e2f/sections" for 127.0.0.1 at 2018-10-25 20:52:33 +0800
Processing by Courses::SectionsController#index as */*
Parameters: {"course_id"=>"5b582404421aa97728ee4e2f"}
MONGODB | localhost:27017 | MagNet_development.find | STARTED | {"find"=>"users", "filter"=>{"_id"=>BSON::ObjectId('56a8b4c9feec620985000000')}, "limit"=>1,
Rendered layouts/admin/_main_sidebar.html.slim (100.1ms)
Rendered layouts/admin/_flash_messages.html.slim (4.7ms)
Rendered layouts/admin/_main_footer.html.slim (3.0ms)
Completed 200 OK in 1147ms (Views: 1120.4ms)