通过js处理删除响应,我通过turbolinks重新加载了curent页面。换句话说:
// onclick setup encapsulates the following
axios({
method: "delete",
url: e.target.getAttribute("href"),
headers: Csrf() // passes the csrf token to keep things 'rails'
}).then(() => {
Turbolinks.visit(locationWithoutHash, { action: "replace" });
});
我们的退出链接就像这样处理。
注销然后重定向到root_url。
问题是,Turbolinks.visit似乎保留了初始方法:
Started DELETE "/auth/sign_out" for 127.0.0.1 at 2018-04-22 11:21:12 +0200
Processing by Devise::SessionsController#destroy as HTML
...stuff
Redirected to http://demodemo.lvh.me:3000/
Completed 302 Found in 11ms (ActiveRecord: 1.9ms)
Started DELETE "/" for 127.0.0.1 at 2018-04-22 11:21:12 +0200
ActionController::RoutingError (No route matches [DELETE] "/"): etc...
这可能只是语法/语法问题,我无法在源代码中找到
如果没有讨论这种行为是否有意义的事实,是否可以为Turbolinks.visit指定一个方法,以确保它有效地通过GET? (因为我只是用它来重新加载当前页面,无论发生什么,它总是一个GET)
答案 0 :(得分:3)
当删除destroy
之后redirect_to request.referer
方法是这样的时候会发生这种情况,或者如果您有这样的任何网址,那么就会发生这样的事情
def destroy
@obj = Model.find(params[:id])
respond_to do |format|
if @obj.destroy
flash[:error] = 'Deleted'
format.html { redirect_to request.referer, status: 303 }
format.js { redirect_to request.referer, status: 303 }
end
end
end
我认为这会有所帮助