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

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – ruby​​ on rails link_to删除方法无法正常工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想使用以下代码删除帖子:
  1. <%= link_to 'Destroy',post,:method => :delete,:onclick => "return confirm('Are you sure you want to delete this post?')" %>

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

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

  1. <%= button_to 'Destroy',method: :delete,:onclick => "return confirm('Are you sure you want to delete this post?')" %>

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

编辑:破坏控制器功能

  1. def destroy
  2. @post = Post.find(params[:id])
  3. @post.destroy
  4.  
  5. respond_to do |format|
  6. format.html { redirect_to posts_url }
  7. format.json { head :no_content }
  8. end
  9. end

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

  1. Started GET "/posts/14" for 127.0.0.1 at 2012-10-21 15:38:28 +0300
  2. Processing by PostsController#show as HTML
  3. Parameters: {"id"=>"14"}
  4. Post Load (0.4ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 14 LIMIT 1
  5. Rendered posts/show.html.erb within layouts/application (0.6ms)
  6. User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  7. Completed 200 OK in 7ms (Views: 5.4ms | ActiveRecord: 0.8ms)
  8. [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

路线:

  1. devise_for :users
  2.  
  3. resources :users
  4.  
  5. resources :posts
  6.  
  7. match '/about' => 'about#index'
  8.  
  9. # You can have the root of your site routed with "root"
  10. # just remember to delete public/index.html.
  11. root :to => 'index#index'
  12.  
  13. # See how all your routes lay out with "rake routes"
  14.  
  15. # This is a legacy wild controller route that's not recommended for RESTful applications.
  16. # Note: This route will make all actions in every controller accessible via GET requests.
  17. # match ':controller(/:action(/:id))(.:format)'
  18. match '*a',:to => 'error#routing'

解决方法

你必须添加
  1. //= require jquery
  2. //= require jquery_ujs

在javascripts / application.js文件

二,检查布局文件,

  1. javascript_include_tag "application"

包含与否?

希望这有帮助.

猜你在找的Ruby相关文章