Ruby on rails link_to remove method not working

I am trying to delete a message using the following code:

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

which doesn't work ... it just redirects me back to the post (posts/:id}

however, if I use the following code, it works

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

can link_to behave like button_to in this case?

EDIT: destroy controller 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 

log when i click destroy button:

 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 

routes:

  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 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' 
+7
source share
6 answers

Decision:

I commented on this from application.js when I created the project

 // 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 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 TO BE PROCESSED, ANY BLANK LINE SHOULD // GO AFTER THE REQUIRES BELOW. // //= require jquery //= require jquery_ujs //= require_tree . 

therefore, re-adding it to the solution

+6
source

You must add

 //= require jquery //= require jquery_ujs 

in javascripts / application.js file

second, check the layout file,

 javascript_include_tag "application" 

includes or not?

I hope for this help.

+12
source

Have you included this line in your layout file?

 <%= javascript_include_tag "application" %> 
+2
source

This is the Rails path:

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

If the message is not deleted, the problem is with your controller. Are you sure you performed the #destroy action #destroy ? What result do you get in server logs?

UPDATE: Change your action to:

 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 
+1
source

Make sure after parameter

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

Check the spaces.

I checked all the comments here. All options were installed correctly. But I noticed that deleting articles does not work, and deleting comments will work. After rewriting the code and checking here and there, I found two files that looked the same in the editor, but one version will work and no one will work!

Curry blog / app / views / articles / index.html.erb:

 <h1>Listing Articles</h1> <%= link_to 'New article', new_article_path %> <table>  <tr>    <th>Title</th>    <th>Text</th>    <th colspan="3"></th>  </tr>  <% @articles.each do |article| %>    <tr>      <td><%= article.title %></td>      <td><%= article.text %></td>      <td><%= link_to 'Show', article_path(article) %></td>      <td><%= link_to 'Edit', edit_article_path(article) %></td>      <td><%= link_to 'Delete', article_path(article), method: :delete, data: { confirm: 'Are you sure?' } %></td>    </tr>  <% end %> </table> 

However, looking at files with xxdiff , I found that one version uses only tabs, while others also use spaces. Perhaps from the code copy code from the tutorial. Therefore, replacing the blanks with tabs fixed the problem.

0
source

All Articles