[Rails Level 1] VIEWS -- Ex

VIEWS SIMPLE

app/views/zombies/show.html.erb

Print out the zombie's name and graveyard.

<% zombie = Zombie.first %>
<h1><%= zombie.name %></h1>

<p>
  <%= zombie.graveyard %>
</p>

 

LINKING

app/views/zombies/show.html.erb

Link to the zombie's show page. Use the zombie's name as the anchor text

<% zombie = Zombie.first %>
<p>
<%= link_to zombie.name, zombie %>
</p>

 

EACH BLOCKS

app/views/zombies/index.html.erb

Use an each block to print the names of all the zombies.

<% zombies = Zombie.all %>
<ul>
<% zombies.all.each do |zombie| %>
  <%= zombie.name%>
<% end %>
</ul>

 

IF

app/views/zombies/index.html.erb

In the each block, if a zombie has more than one tweet, print out SMART ZOMBIE.

<% zombies = Zombie.all %>
<ul>
  <% zombies.each do |zombie| %>
    <li>
      <%= zombie.name %>
      <% if zombie.tweets.length > 1 %>
        <%= "SMART ZOMBIE" %>
      <% end %>
    </li>
  <% end %>
</ul>

 

LINKING IN BLOCKS

app/views/zombies/index.html.erb

In the each block, make the zombie's name link to its edit page.

<% zombies = Zombie.all %>
<ul>
  <% zombies.each do |zombie| %>
    <li>
      <%= link_to zombie.name, edit_zombie_path(zombie) %>
    </li>
  <% end %>
</ul>

 

posted @ 2014-09-11 06:12  Zhentiw  阅读(256)  评论(0)    收藏  举报