gpt4 book ai didi

ruby-on-rails - Rails,def destroy,没有响应 destroy.js.erb,为什么?

转载 作者:行者123 更新时间:2023-12-04 18:51:22 27 4
gpt4 key购买 nike

Rails,def destroy,没有响应 destroy.js.erb

这是我的方法:

  # DELETE /Groups/1
# DELETE /Groups/1.xml
def destroy

@group = Group.find(params[:id])
@group.destroy

respond_to do |format|
format.js
end
end

在我看来:
<a href="/groups/122" data-confirm="Are you sure?" data-method="delete" rel="nofollow" >Delete</a>

但是删除日志显示:


tarted POST "/groups/128" for 127.0.0.1 at Fri Apr 22 22:21:31 -0700 2011
Processing by GroupsController#destroy as HTML
Parameters: {"authenticity_token"=>"J+A2DN87qoigNxw97oK6NWqPQvXt7KAwLMAM7Er/eWM=", "id"=>"128"}
.....
Completed 406 Not Acceptable in 372ms

destory.js.erb 永远不会被调用。任何想法为什么?谢谢

最佳答案

好吧,这里有几个问题:

第一的,

<a href="/groups/122" data-confirm="Are you sure?" data-method="delete" rel="nofollow" >Delete</a> 

这个链接不是远程的,你可以在你提供的日志中看到它:
Processing by GroupsController#destroy as HTML

要使您的链接提交 ajax 请求,请添加 :remote => true (与您已经拥有的方式相同 :confirm => 'Are you sure?' 和 :method => :destroy )

其次,您应该在使用 javascript 响应时禁用布局渲染。

因此,您的操作可能如下所示:
  respond_to do |format|
format.js { render :template => 'groups/destroy.js.erb', :layout => false }
end

为方便起见,我已将其添加到我的 Controller 中:
layout Proc.new { |controller| controller.request.xhr?? false : 'application' }

因此如果请求是 xhr 类型,则不会呈现布局。然后你可以保持现在的 Action ,它应该可以工作

关于ruby-on-rails - Rails,def destroy,没有响应 destroy.js.erb,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5762374/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com