铁轨3 – link_to销毁不工作

我试图创build一个销毁链接到我的用户控制器,我也使用devise。

这是我的代码 –

视图

<%= 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。

其实我昨天也遇到了同样的问题

尝试这个:

 <%= button_to "delete", your_object, :method=>:delete, :class=>:destroy %> 

它工作(至less对我来说)

如果您使用的是jQuery而不是Prototype,那么您可能会丢失一个JavaScript文件。

你可以从jquery-ujs GitHub页面或Railscasts的第205集find有关如何将其添加到项目的详细信息。

在一个猜测,我认为这是因为在Rails 3中,不引人注意的JavaScript现在用于这样的function(Rails 2会输出一堆讨厌的内联JavaScript为您的代码,Rails 3将JavaScript放在一个外部文件,并使用HTML5 data-属性与之交互。)

为了解决这个问题,你需要在你的页眉中包含<%= csrf_meta_tags %>来引用外部的javascript。 它也处理XSS问题。

这里的一些细节: 删除链接在Rails 3视图中发送“Get”而不是“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 $ // eg $jQ('#div_id').stuff instead of $('#div_id').stuff var $jQ = jQuery.noConflict(); </script> 
  1. 请按照安装部分rails / jquery-ujs中的步骤进行操作
  2. 在布局文件中添加<%= javascript_include_tag“应用程序”%>“

如果您的应用程序中没有包含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 %>