这是我的删除链接:
<%= link_to "delete", client, data: {
"turbo-method": :delete,
"turbo-confirm": "Are you sure?" } %>
这是使用Minitest/Capibara/Selenium/Rrails 7:进行的系统测试
test "delete client" do
accept_confirm do
first("ul.clients li#client-#{@client.id} span.client-controls a").click
end
assert_selector "div.alert-success", text: "Client is deleted!"
end
当我运行测试时,我得到ActionController::RoutingError:No route matches[DELETE]"/">
当我尝试时:
puts first("ul.clients li#client-#{@client.id} span.client-controls a")["href"]
我得到了正确的url(/clients/123(。我读到关于这个问题,Capybara不接受涡轮增压方法,但它应该通过以下方式解决:https://github.com/teamcapybara/capybara/commit/cc451e77b2fb6588e1d00814d0361e63bb66b86c
我使用的是Rails 7、水豚(3.37.1(、硒网络驱动程序(4.1.0(、minitest(5.15.0(
编辑以获取更多信息:刚刚检查了test.log:
Started DELETE "/clients/298486374" for 127.0.0.1 at 2022-09-10 13:54:24 +0200
Processing by ClientsController#destroy as TURBO_STREAM
Parameters: {"id"=>"298486374"}
删除查询工作正常吗…
Redirected to http://127.0.0.1:44597/
Completed 302 Found in 10ms (ActiveRecord: 1.2ms | Allocations: 1962)
Started DELETE "/" for 127.0.0.1 at 2022-09-10 13:54:25 +0200
为什么删除"/"重定向后请求????
我在ClientsController:中的销毁操作
def destroy
get_client.destroy
flash[:success] = "Client is deleted!"
redirect_to root_url
end
private
def get_client
Client.find(params[:id])
end
所以。。。是的。。。我想我太快了,没有在这里发布问题。。。但是,对于其他有同样问题的人来说,这里有一个解决方案:
我刚刚在销毁操作中在redirect_to root_url
之后添加了status: :see_other
,现在它工作得很好。
我从这篇旧帖子中得到了这个想法:https://makandracards.com/makandra/38347-redirecting-responses-for-patch-or-delete-will-not-redirect-with-get
仍然不确定它为什么会这样工作,如果有人想捐款,请捐款。