Blog Delete Action Error?
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_fil开发者_开发百科ter :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 DELETE
request when you click the link. If the javascript isn't working, it will fall back to doing a GET
which will render the show
action.
Make sure you have rails.js
and prototype.js
in 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 of rails.js
.
This line
format.html { redirect_to(articles_url) }
should send it back to your index action, where do you want to end up?
This case happen also in my project.
Finnaly I know the cause. It was because my application.js that located in app/assets/javascript
was being replaced by another application.js
that I copy when I try to explore about bootstap.
I simply add this 3 rows back:
//= require jquery
//= require jquery_ujs
//= require jquery-ui
to the application.js
on the top row its okay.
and put this
on my app/views/layout/application.html.erb
n it work..
in case if the jquery have been install n still error like me :D
sory for bad english :)
精彩评论