I trying to learn rails by doing some labs in railsforzombies, I am in lab3 (if statements).
It has two tables:
Objective In the each block, if a Zombie has more than 1 tweet, print out SMART ZOMBIE
<% zombies = Zombie.all %>
<ul>
<% zombies.each do |zombie| %>
<li>
<%= zombie.name %>
# add if statement here
</li>
<% end %>
</ul>
I have tried some solutions but I get it wrong.
Conditionals are formed using a combination of if statements and comparison and logical operators (<, >, <=, >=, ==, != , &&, ||) . They are basic logical structures that are defined with the reserved words if , else , elsif , and end .
Notice Ruby uses elsif, not else if nor elif. Executes code if the conditional is true. If the conditional is not true, code specified in the else clause is executed.
In if statement, the block executes once the given condition is true, however in unless statement, the block of code executes once the given condition is false.
<% if zombie.tweets.size > 1 %>
Smart Zombie!
<% end %>
<%= 'SMART ZOMBIE' if zombie.tweets.size > 1 %>
Note: count/length/size are all subtly different.
Edit: more than 1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With