ruby-on-rails - 使用每个块来打印所有僵尸的名称

原文 标签 ruby-on-rails for-loop zombie-process

Use an each block to print the names of all the Zombie

I'm trying to do the 'rails for zombies' course on codeschool, but I'm stuck on this one:

http://railsforzombies.org/levels/3/challenges/3

The frustrating thing about this course, is that you can't refine your answers, because you don't see the return value from incorrect solutions.

Anyway, the database looks like this:

id  name    graveyard
1   Ash     Glen Haven Memorial Cemetary
2   Bob     Chapel Hill Cemetary
3   Jim     My Fathers Basement

The challenge is to "Use an each block to print the names of all the Zombies"

My solution is: <% zombies.each do |z| puts z.name end
%>

This fails, but I don't get to see how it fails, so I can't refine it. What is the solution?

Answer

Try this:

<% zombies.each do |z| %>
  #note the equal-to sign below. this means that the text will be sent as output
  <%= z.name %>
<% end %>

Notes:

  • puts is used to output the text to the console
  • <%= %> is the way to output text in an ERB template

翻译

我正在尝试在Codeschool上进行“僵尸攻略”课程,但我坚持这一点:

http://railsforzombies.org/levels/3/challenges/3

此课程令人沮丧的是,您无法优化答案,因为您看不到错误解决方案的返回值。

无论如何,数据库看起来像这样:

id  name    graveyard
1   Ash     Glen Haven Memorial Cemetary
2   Bob     Chapel Hill Cemetary
3   Jim     My Fathers Basement


面临的挑战是“使用每个块来打印所有僵尸的名称”

我的解决方案是:
    <%
     zombies.each做| z |
         放z.name
     结束
    %>

这失败了,但是我看不到它是怎么失败的,所以我无法完善它。
解决办法是什么?
最佳答案
尝试这个:

<% zombies.each do |z| %>
  #note the equal-to sign below. this means that the text will be sent as output
  <%= z.name %>
<% end %>


笔记:


puts用于将文本输出到控制台
<%= %>是在ERB模板中输出文本的方法
相关推荐

ruby-on-rails - Rails 3教程10.4.2:NoMethodError未定义方法'admin?'对于nil:NilClass

ruby-on-rails - 单表继承来引用具有自己字段的子类

ruby-on-rails - 如何在Rails应用程序中强制gem版本?

ruby-on-rails - 滑轨。群发邮件

ruby-on-rails - OmniAuth无效响应错误

ruby-on-rails - 当我的同事运行“捆绑安装”时,他会安装最新的宝石还是Gemfile.lock中的宝石?

ruby-on-rails - 如何按升序对红宝石数组进行排序,但最后保持零

ruby-on-rails - 具有多个命名空间的Rails应用

ruby-on-rails - Ruby on Rails:自定义操作

ruby-on-rails - Ruby on Rails:如何总结数据库中的这些元素?