删除comments
一个blog的另外一个重要的特征就是能够删除多余的comments。为了做到这个,我们需要在视图中实现一系列的代码,以及在CommentsController中实现destroy的action。
因此首先,让我们来增加删除的链接,在局部文件中 app/views/comments/_comment.html.erb:
<p> <strong>Commenter:</strong> <%= comment.commenter %> </p>
<p> <strong>Comment:</strong> <%= comment.body %> </p>
<p> <%= link_to 'Destroy Comment', [comment.article, comment], method: :delete, data: { confirm: 'Are you sure?' } %> </p> |
点击这个新的链接“Destroy Comment”将会发送一个 DELETE /articles/:article_id/comments/:id 到我们的CommentsController,用这个来找到我们想要删除的comment,因此让我们在我们的控制器中来增加一个destroy的action app/controllers/comments_controller.rb:
classCommentsController < ApplicationController defcreate @article= Article.find(params[:article_id]) @comment= @article.comments.create(comment_params) redirect_to article_path(@article) end
defdestroy @article= Article.find(params[:article_id]) @comment= @article.comments.find(params[:id]) @comment.destroy redirect_to article_path(@article) end
private defcomment_params params.require(:comment).permit(:commenter, :body) end end |
这个destroy的action将会找到我们正在寻找的article,在@article.comments的集合中定位到这个comment,然后从数据库中去删除它,最后我们在返回到显示article的界面上。
original text: http://guides.rubyonrails.org/getting_started.html#deleting-comments
相关推荐
Now in version 4, Rails is mature and powerful, and to use it effectively you need more than a few Google searches. You'll find no substitute for the guru's-eye-view of design, testing, deployment, ...
Ruby on Rails helps you produce high-quality, beautiful-looking web applications quickly. You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of ...
Beginning Rails 4
Rails test described by RSpec
Rails 4 Test Prescriptions.pdf
Rails 4 in Action, Second Edition.pdf
一本Rails 4开发进阶教程,适合有一定开发经验的Ruby on Rails开发人员阅读
唔,1分应该还是有人下的吧,共同学习进步,Ruby on Rails is an open source web framework.... "Rails 4 in Action" is a fully-revised second edition of "Rails 3 in Action." This hands-on, compreh...
学习ruby!
[Pragmatic Bookshelf] Crafting Rails 4 Applications Expert Practices for Everyday Rails Development (E-Book) ☆ 图书概要:☆ Get ready to see Rails as you've never seen it before. Learn how to ...
Crafting Rails 4 Applications
Ruby on Rails4 api.chm ruby on rails 4 最新api chm版本,方便大家阅览!
Book Details Publisher: Addison-Wesley Professional By: Obie Fernandez ISBN-10: 0321944275 ISBN-13: 978-0321944276 Year: June 8, 2014 Pages: 912 Language: English File size: 4 MB File format: PDF
Ruby on Rails框架是Ruby的一个杀手级应用框架,可以敏捷开发Web应用,是目前为止开发效率最高的Web开发技术
Rails 4 Test Prescriptions is a comprehensive guide to how tests can help you design and write better Rails applications. In this completely revised edition, you'll learn why testing works and how to...
一个用Ruby on Rails搭建的图片分享的网站项目.完整源代码
rails4gems.part3.rar rails4常用gem3