I have a blog style site and under each article there is the usual ‘view’ ‘edit’ and ‘delete’ functions for users once they are signed in. The issue is when I click on ‘delete’ the link it sends you to the ‘view’ article page and doesn’t delete the article. Can’t figure this one out as the delete and view paths are different in my code but do the same thing…??
class ArticlesController < ApplicationController
before_filter :authenticate_user!, :except => [:index, :show]
# GET /articles
# GET /articles.xml
# display the amount of article on the home page
def index
@articles = Article.published.page(params[:page]).per(6).ordered
respond_to do |format|
format.html # index.html.erb
# format.atom #index.atom.builder
format.xml { render :xml => @articles }
end
end
# GET /articles/1
# GET /articles/1.xml
def show
@article = Article.find(params[:id])
@comment = Comment.new(:article=>@article)
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @article }
end
end
...
/controllers/articles_controller
# DELETE /articles/1.xml
def destroy
@article = Article.find(params[:id])
authorize! :destroy, @article
@article.destroy
respond_to do |format|
format.html { redirect_to(articles_url) }
format.xml { head :ok }
end
end
end
views/articles/_article.html.erb
<div class= "art-links">
<%= link_to 'Read', article %>
<% if can? :update, article %>
| <%= link_to 'Edit', edit_article_path(article) %> |
<% end %>
<% if can? :destroy, article %>
<%= link_to 'Delete', article, :confirm => 'Are you sure?', :method => :delete %>
<% end %>
</div>
<br />
This may be because you don’t have the right javascript files set up. Rails 3 uses unobtrusive javascript to create the
DELETErequest when you click the link. If the javascript isn’t working, it will fall back to doing aGETwhich will render theshowaction.Make sure you have
rails.jsandprototype.jsin your layout. If you are using a javascript framework other than Prototype, you will need to have that framework (jQuery or whatever) along with the appropriate port ofrails.js.