我们的edit页面和new页面非常相似,事实上,他们共同共享着相同的代码用来显示form。 让我们用一个局部视图来剔除重复的代码。按照惯例,局部文件的前缀是一下划线。
ps. 想了解更多的局部文件信息,可以参考Layouts and Rendering in Rails
创建一个新的文件app/views/articles/_form.html.erb,编辑如下的内容:
<%= form_for @article 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 %> |
对于 form_for 所声明的都是和原来的一模一样的。我们使用这个更短的,更简单的 form_for 声明 去替代其他的form,这么做的原因是@article是一个对于完整RESTful路由集的资源映射,Rails能够推断出来哪个URI或者哪个方法能够被使用。更多的关于form_for用法,可以参考Resource-oriented style.
现在,让我们来修改视图app/views/articles/new.html.erb,用到这个新的局部视图,重新编辑它:
<h1>New article</h1>
<%= render 'form' %>
<%= link_to 'Back', articles_path %> |
同样的处理这个视图:app/views/articles/edit.html.erb
<h1>Edit article</h1>
<%= render 'form' %>
<%= link_to 'Back', articles_path %> |
原文http://guides.rubyonrails.org/getting_started.html#using-partials-to-clean-up-duplication-in-views
—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, ...
8 Setting up a Todo application in Rails 8 Analyzing folder structure of a Rails application 10 Creating views 12 Styling views using CSS 14 Redesigning the Todo application 16 Challenges in styling ...
Rails 4 in Action, Second Edition.pdf
Best practices on how to apply Rails continue to change, and this edition keeps up. Examples use Concerns, Russian Doll caching, and Turbolinks, and the book focuses throughout on the right way to ...
Deploying Rails with Docker, Kubernetes and ECS shows you how to set up the project, push it to DockerHub, manage services and set up an efficient continuous integration environment. Every concept is ...
唔,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...
This short book is aimed at existing Rails developers who are looking to quickly get up to speed with the changes in the new Rails 5 framework. Table of Contents Chapter 1: Upgrading to Rails 5 ...
See how creating an advanced Postgres index for a case-insensitive search speeds up your back end - enabling you to create a dynamic user experience using Angular 2. Create reusable components that ...
In this completely revised edition, you'll learn why testing works and how to test effectively using Rails 4, Minitest 5, and RSpec 3, as well as popular testing libraries such as factory_girl and ...
This second edition of the bestselling Crafting Rails Applications has been updated to Rails 4 and discusses new topics such as streaming, mountable engines, and thread safety. ☆ 出版信息:☆ [作者...
It teaches you how to set up the Bootstrap framework, and will showcase the true power of your Rails app using Bootstrap's grid system. You will learn about other Bootstrap components and move on to ...
Detection of critical defects in rails using ultrasonic surface wave
Rails 3 in Action 2011年9月新鲜出炉,针对最新的Rails 3.1进行说明
Beginning Rails 4
Rails 4 Test Prescriptions.pdf
Rails test described by RSpec
Ruby on Rails Guides_ A Guide to Testing Rails Applications.pdf
The more confident reader might be able to get started in Rails using just this book, extensive online resources, and his wits, but there are other publications that are more introductory in nature ...