ruby on rails link_to删除方法无法正常工作

我想使用以下代码删除post:

 :delete, :onclick => "return confirm('Are you sure you want to delete this post?')" %> 

这不起作用……它只是将我重定向回post(posts/:id}

但是,如果我使用以下代码,它的工作原理

  "return confirm('Are you sure you want to delete this post?')" %> 

在这种情况下,是否可以使link_to表现为button_to

编辑:破坏控制器function

  def destroy @post = Post.find(params[:id]) @post.destroy respond_to do |format| format.html { redirect_to posts_url } format.json { head :no_content } end end 

单击“销毁”按钮时记录:

 Started GET "/posts/14" for 127.0.0.1 at 2012-10-21 15:38:28 +0300 Processing by PostsController#show as HTML Parameters: {"id"=>"14"} Post Load (0.4ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 14 LIMIT 1 Rendered posts/show.html.erb within layouts/application (0.6ms) User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1 Completed 200 OK in 7ms (Views: 5.4ms | ActiveRecord: 0.8ms) [2012-10-21 15:38:28] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true 

路线:

  devise_for :users resources :users resources :posts match '/about' => 'about#index' # You can have the root of your site routed with "root" # just remember to delete public/index.html. root :to => 'index#index' # See how all your routes lay out with "rake routes" # This is a legacy wild controller route that's not recommended for RESTful applications. # Note: This route will make all actions in every controller accessible via GET requests. # match ':controller(/:action(/:id))(.:format)' match '*a', :to => 'error#routing' 

你必须添加

 //= require jquery //= require jquery_ujs 

在javascripts / application.js文件中

二,检查布局文件,

 javascript_include_tag "application" 

包含与否?

希望这有帮助。

解:

我在创建项目时从application.js中注释掉了这个

 // This is a manifest file that'll be compiled into application.js, which will include all the files // listed below. // // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path. // // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the // the compiled file. // // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD // GO AFTER THE REQUIRES BELOW. // //= require jquery //= require jquery_ujs //= require_tree . 

所以重新添加它解决了问题

您是否在布局文件中包含此行?

 <%= javascript_include_tag "application" %> 

这是Rails方式:

<%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete %>

如果post未删除,则问题出在您的控制器中。 您确定已正确实施#destroy操作吗? 您在服务器日志中获得的输出是什么?

更新:将您的操作更改为:

 def destroy @post = Post.find(params[:id]) respond_to do |format| if @post.destroy format.html { redirect_to posts_url } format.json { head :no_content } else format.html # do something here format.json { head :no_content } end end end 

确保参数后没有空格

 def destroy @post = Post.find(params[:id]) @post.destroy redirect_to posts_path, :notice => "Your post has been deleted successfully." end 

检查空格。

我在这里查看了所有评论。 所有包含都设置正确。 但我注意到删除文章不起作用,删除评论会起作用。 在重写了一些代码并在这里和那里进行检查之后,我发现在编辑器中看起来相同的两个文件,但是一个版本可以工作而一个版本无效!

咖喱博客/应用/视图/用品/ index.html.erb:

 

Listing Articles

<%= link_to 'New article', new_article_path %> <% @articles.each do |article| %> <% end %>
Title Text
<%= article.title %> <%= article.text %> <%= link_to 'Show', article_path(article) %> <%= link_to 'Edit', edit_article_path(article) %> <%= link_to 'Delete', article_path(article), method: :delete, data: { confirm: 'Are you sure?' } %>

但是,使用xxdiff查看文件时,我发现在一个版本中只使用了选项卡,而另一个版本也使用了空白。 可能来自教程中的复制粘贴代码。 因此,用标签替换空白确实解决了问题。