![](/img/trans.png)
[英]Rails 4 link_to Destroy not working in Getting Started tutorial
[英]rails 3 - link_to to destroy not working
我正在尝试创建一个销毁链接到我的用户控制器,我也在使用设计。
这是我的代码 -
<%= link_to 'Delete User?', child, :confirm => "Are you sure you want to delete #{child.full_name}?", :method => :delete, :class => "user-additional", :style => "font-size:12px;font-weight:normal;" %>
def destroy
if @user = User.find(params[:id])
@user.destroy
respond_to do |format|
format.html { redirect_to account_index_path }
format.xml { head :ok }
end
end
end
devise_for :users
resources :users, :except => [:new]
该链接转换为localhost:3000 / users / 10
单击此按钮将打开用户显示而不是删除它们
有任何想法吗 ?
破坏性行为应作为表格提交执行 - http://www.w3.org/2001/tag/doc/whenToUseGet.html#checklist
使用button_to
(传递:method => :delete
)来适当地设置按钮的样式。
实际上我昨天遇到了完全相同的问题
尝试这个:
<%= button_to "delete", your_object, :method=>:delete, :class=>:destroy %>
它起作用(至少对我来说)
如果您使用的是jQuery而不是Prototype,则可能缺少javascript文件。
您可以从jquery-ujs GitHub页面或Railscasts的第205集中找到有关如何将其添加到项目的详细信息。
猜测我认为这是因为在Rails 3中,不显眼的javascript现在用于此类功能(Rails 2会为您的代码输出一堆令人讨厌的内联javascript,Rails 3将javascript放在外部文件中,并使用HTML5 data-
attributes与之交互。)
要解决此问题,您需要在页眉中包含<%= csrf_meta_tags %>
以引用外部JavaScript。 它还涉及XSS问题。
这里有一些细节: 删除链接在Rails 3视图中发送“Get”而不是“Delete”
如果你没有在你的应用程序中包含jquery和jquery-ujs,默认的link_to默认随脚手架一起工作!
我有同样的问题。包括这两个js后它解决了!
此外,如果您在生产模式中遇到此问题,可能是因为您尚未编译资产。 请参阅http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets
为我工作的确认信息。
<%= button_to 'Destroy', {action: :destroy, id: version.id}, onclick: 'return confirm("Are you sure?")', method: :delete %>
如果您使用的是jQuery,请确保您拥有以下内容:
<script type="text/javascript">
// this allows jquery to be called along with scriptaculous and YUI without any conflicts
// the only difference is all jquery functions should be called with $j instead of $
// e.g. $jQ('#div_id').stuff instead of $('#div_id').stuff
var $jQ = jQuery.noConflict();
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.