`
jiajie0531
  • 浏览: 29555 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

rails4 5.12 Using partials to clean up duplication in views

 
阅读更多

我们的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

分享到:
评论

相关推荐

    Rails 4 in Action, Second Edition.pdf

    ### Rails 4 in Action, 第二版:关键知识点解析 #### 一、Rails 4简介与新特性 **Rails 4 in Action, 第二版** 是一本深入介绍Ruby on Rails框架的专业书籍。该书由Ryan Bigg、Yehuda Katz、Steve Klabnik和...

    Agile Web Development with Rails 4

    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 ...

    Rails 4 in Action

    唔,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 3 in Action

    《Rails 3 in Action》是2011年由Ryan Bigg撰写的一本关于Ruby on Rails框架的权威指南,专门针对当时最新的Rails 3.1版本进行了深入解析。这本书旨在帮助开发者充分利用Rails 3.1的强大功能,提升Web应用开发的效率...

    Rails.Angular.Postgres.and.Bootstrap.2nd.Edition

    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 ...

    Rails 4 Test Prescriptions(Pragmatic,2014)

    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 ...

    The Rails 4 Way

    ### 关于《The Rails 4 Way》的知识点总结 #### 标题:The Rails 4 Way 这本书主要讲述了Ruby on Rails 4版本的核心特性和最佳实践。Ruby on Rails(简称Rails)是一个用Ruby语言编写的开源全栈Web应用框架。本书...

    [Rails] Crafting Rails 4 Applications (英文版)

    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. ☆ 出版信息:☆ [作者...

    Bootstrap for Rails

    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 ...

    Agile Web Development with Rails Final

    - Rails is designed to support agile methodologies, allowing teams to iterate quickly, deliver value early, and adapt to changing requirements. Its flexible structure enables developers to implement ...

    Agile Web Development with Rails 4th edition(敏捷Web开发与Rails:程序指南 第四版)

    本书旨在帮助开发者充分利用Rails 4的特性,提高开发效率,实现快速迭代和高质量的代码编写。 Rails是一个强大的、基于模型-视图-控制器(MVC)架构模式的开源Web应用框架,它以Ruby编程语言为基础,强调简洁和生产力...

    Ruby on Rails Guides_ A Guide to Testing Rails Applications.pdf

    综上所述,《Ruby on Rails Guides_ A Guide to Testing Rails Applications.pdf》是一个全面的资源,无论你是Rails新手还是资深开发者,都能从中学习到如何为Rails应用编写高质量的测试。从理论到实践,从单元测试...

    Crafting Rails 4 Applications

    ### 关于《Crafting Rails 4 Applications》的关键知识点解析 #### 标题解析:Crafting Rails 4 Applications - **Rails 4**:Rails 4是Ruby on Rails框架的一个版本,该版本在2013年发布。Ruby on Rails(简称...

Global site tag (gtag.js) - Google Analytics