我们已经完成了CRUD中的“CR”部分了。现在让我们关注“U”这部分,更新articles。
第一步我将要做的是,在ArticlesController中增加一个action edit。
defedit @article= Article.find(params[:id]) end |
这个视图将会包含一个form,类似于我们用过的那个,在我们创建新的articles的时候。创建一个文件app/views/articles/edit.html.erb,编辑如下:
<h1>Editing article</h1>
<%= form_for :article, url: article_path(@article), method: :patch do |f| %> <% if @article.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:</h2> <ul> <% @article.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <p> <%= f.label :title %><br> <%= f.text_field :title %> </p>
<p> <%= f.label :text %><br> <%= f.text_area :text %> </p>
<p> <%= f.submit %> </p> <% end %>
<%= link_to 'Back', articles_path %> |
此刻,我们把这个form定位到action update,其还没有被定义, 但接下来就会被实现的
method: :patch这个属性告知Rails,我们想让这个form通过PATCH HTTP方法来提交,HTTP方法就是你所期待的用来update资源,通过REST协议。
form_tag中的第一个参数是一个对象,@article,会引起helper帮助在form中填充对象的字段。用相同的名字来传入一个标签(:article),正如这个实例变量(@article)神奇地指向相同的行为。这就是现在发生的。更多的细节可以在参考form_for documentation.
接下里,我们需要创建action update,在这个文件里app/controllers/articles_controller.rb:
defupdate @article= Article.find(params[:id])
if@article.update(article_params) redirect_to @article else render 'edit' end end
private defarticle_params params.require(:article).permit(:title, :text) end |
当你想要修改一条已存在的记录时,新的方法update会被使用,它接收一个hash,里面包含了你想要更新的那些特性。比如放在以前,如果在更新article时有个错误,我们会返回form,显示错误信息给用户看。
我们复用方法 article_params,这个我们前面在action create的时候已经定义过了。
information:你不需要为了更新而传入所有参数。比如,如果你调用了@article.update(title: ‘A new title’), Rails将仅仅更新特性title,不处理其他的特性。
最后,在罗列所用的articles中,我们想要显示一个链接到action edit,因此让我们现在来增加吧,app/views/articles/index.html.erb 在这里显示链接“Show”:
<table> <tr> <th>Title</th> <th>Text</th> <th colspan="2"></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> </tr> <% end %> </table> |
同样的,在文件app/views/articles/show.html.erb 中,我们也会增加链接,因此在article的页面也会有一个“Edit”的链接。在模板的底部增加这个链接:
...
<%= link_to 'Back', articles_path %> | <%= link_to 'Edit', edit_article_path(@article) %> |
下面就是我们的程序看上去的效果:
原文:http://guides.rubyonrails.org/getting_started.html
--end
相关推荐
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 ...
Rails 4 Test Prescriptions.pdf
Beginning Rails 4
Rails test described by RSpec
Rails 4 in Action, Second Edition.pdf
[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 ...
唔,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...
一本Rails 4开发进阶教程,适合有一定开发经验的Ruby on Rails开发人员阅读
学习ruby!
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 Rails4 api.chm ruby on rails 4 最新api chm版本,方便大家阅览!
Crafting Rails 4 Applications
Ruby on Rails框架是Ruby的一个杀手级应用框架,可以敏捷开发Web应用,是目前为止开发效率最高的Web开发技术
todo-rails4-angularjs, Rails 4和AngularJS集成的展示 Rails 4和AngularJS集成的展示这是一个待办事项列表管理应用程序,用 Rails 4和AngularJS编写。 任务支持CRUD操作以及drag&拖放排序和通过弹出式日历设置的...
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搭建的图片分享的网站项目.完整源代码
rails4gems.part3.rar rails4常用gem3