问题
I am trying to delete a post using the code below:
<%= link_to 'Destroy', post, :method => :delete, :onclick => "return confirm('Are you sure you want to delete this post?')" %>
which does not work... it simply 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?')" %>
is it possible to make link_to
behave as 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 the 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'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'
回答1:
You must be add
//= require jquery
//= require jquery_ujs
in javascripts/application.js file
second, check in layout file,
javascript_include_tag "application"
is include or not?
Hope this help.
回答2:
Solution:
I had commented out 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'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 .
so re-adding it in solved the problem
回答3:
Have you included this line in your layout file?
<%= javascript_include_tag "application" %>
回答4:
This is the Rails way:
<%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete %>
If the post isn't deleted then the problem lies in your controller. Are you sure you've correctly implemented the #destroy
action? What's the output you're getting in your 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
回答5:
Make sure that there is no space after the param
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path, :notice => "Your post has been deleted successfully."
end
回答6:
Check the whitespaces.
I checked all the comments here. All the includes were set correctly. But I noticed that deleting articles did not work, while deleting comments would work. After rewriting some code and checking here and there, I found two files that looked identically in the editor, but one version would work and one would not 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 the files with xxdiff
I found that in one version only tabs where used, while the other also used blanks. Probably, from copy pasting code from the tutorial. Replacing the blanks with tabs did therefore fix the problem.
来源:https://stackoverflow.com/questions/12997167/ruby-on-rails-link-to-delete-method-not-working