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

rails4 7 Refactoring

阅读更多

重构

现在我们已经有可以操作的articles和comments,来看一下模板app/views/articles/show.html.erb。它变得越来越冗长和笨拙。我们可以用局部文件来清理.

 

7.1 Rendering Partial Collections

首先,我们来建一个comment的局部文件, 把用来显示article所有的comment提取出来。创建这个文件 app/views/comments/_comment.html.erb,编辑上下面的代码:

<p>

  <strong>Commenter:</strong>

  <%= comment.commenter %>

</p>

 

<p>

  <strong>Comment:</strong>

  <%= comment.body %>

</p>

接着你可以编辑文件 app/views/articles/show.html.erb:

<p>

  <strong>Title:</strong>

  <%= @article.title %>

</p>

 

<p>

  <strong>Text:</strong>

  <%= @article.text %>

</p>

 

<h2>Comments</h2>

<%= render @article.comments %>

 

<h2>Add a comment:</h2>

<%= form_for([@article, @article.comments.build]) do |f| %>

  <p>

    <%= f.label :commenter %><br>

    <%= f.text_field :commenter %>

  </p>

  <p>

    <%= f.label :body %><br>

    <%= f.text_area :body %>

  </p>

  <p>

    <%= f.submit %>

  </p>

<% end %>

 

<%= link_to 'Edit Article', edit_article_path(@article) %> |

<%= link_to 'Back to Articles', articles_path %>

对于每一个comment,这将会跳转到局部文件 app/views/comments/_comment.html.erb,而@article.comments是一个数组集合。作为跳转的方法会逐个迭代@article.comments的数组集合,对于每一个comment都赋值一个本地变量,名称都是同一个局部文件,在这个例子中comment在稍后可以使用,会给我们显示出结果来。

 

 

original text: http://guides.rubyonrails.org/getting_started.html#refactoring

0
0
分享到:
评论

相关推荐

    Addison.Wesley.Rails.AntiPatterns.Best.Practice.Ruby.on.Rails.Refactoring

    Addison.Wesley.Rails.AntiPatterns.Best.Practice.Ruby.on.Rails.Refactoring

    [Agile.Web.Development.with.Rails]4rd英文版(清晰文字版pdf+源码)

    神级ROR教程第四版:最新版 传说中学Ruby只要两本书,一本是ProgrammingRuby(在我的资源列表里也有),另一本就是Agile.Web.Development.with.Rails,即该本书。... Instead, it's a complete refactoring.

    Refactoring: Ruby Edition

    在此摘抄官方的介绍: This book will help you 这本书会帮助你: • Understand the core principles of refactoring and the reasons for doing ...• Successfully refactor Ruby on Rails code 成功的重构 ROR 代码

    enterprise_rails.pdf

    4. Database As a Fortress .................................................. 41 Your Database Is an Application, Too 42 “One Framework to Rule Them All” 42 “No One in My Company Will Ever Write a ...

    仿知乎java源码-refactoring-to-rails:将Spring/Hibernate应用程序重构为Rails的示例

    Rails 这是一个演示如何重构遗留 Java 项目以使用 Rails 的项目。 整个存储库历史旨在从头到尾读取。 每个提交消息都用该特定步骤的目标和预期进行注释。 如果您只是从特定快照复制或模仿代码而不了解它是如何到达...

    refactoring-fortune-teller

    使用动态路由重构算命先生 有关该项目的。 设置 通过运行bin/server启动Web bin/server 。 导航到实时应用程序预览。 在工作时,请记住导航到/git并在工作时经常提交。... rails grade以确保一切仍然有效。

    refactoring-workshop:重构研讨会

    setup.rb必要的代码app.rb运行(模拟了一个超级基本的Rails)我们不需要看/编辑它,除非只是好奇。 :-) tests.rb自我解释。 是日常生活中必需的,特别是用于重构。 运行bundle以确保您具有运行测试所需的gem( ...

    refactoring-msm-queries-1

    使用方法重构MSM查询有关该项目的。设置通过运行bin/server启动Web bin/server 。 导航到实时应用程序预览。 在工作时,请记住导航到/git并在工作时经常提交。 rails grade以确保一切仍然有效。

    refactoring-msm-queries-2

    重构MSM查询-再次! 在这个项目中,我们将使用ActiveRecord强大的关联帮助器来重新编写关联访问器方法。 使用此工具可以帮助您计划新的关联方法: ...重新访问您的页面和rails grade以确保一切正常。

    Wrox.Professional.Enterprise.dot.NET.Oct.2009.pdf

    Also covers enterprise design patterns: MVC including Ruby on Rails, Monorail, and ASP.NET MVC, MVP, observer, and more Contains a primer on object-oriented design Professional Enterprise .NET ...

Global site tag (gtag.js) - Google Analytics