if statement in rails - ruby-on-rails

I trying to learn rails by doing some labs in railsforzombies, I am in lab3 (if statements).
It has two tables:
Zombies{id, name, graveyard}
Tweets{id, status, zombie_id}
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.

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

This works!!
<% zombies = Zombie.all %>
<ul>
<% zombies.each do |zombie| %>
<li>
<%= zombie.name %>
<%= 'SMART ZOMBIE' if zombie.tweets.count > 1 %>
</li>
<% end %>
</ul>

This should work:
<ul>
<% zombies.each do |zombie| %>
<li>
<%= link_to zombie.name, edit_zombie_path(zombie) %>
<%= 'Smart Zombie' if zombie.tweets.count > 1 %>
</li>
<% end %>
</ul>

I was alternately able to make the following work and pass on, would love to hear if this is bad or not.
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 %>
if zombie > 1 tweet put "SMART ZOMBIE"
</li>
<% end %>
</ul>

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

<% if zombie.tweets.count > 1 %>
<em> SMART ZOMBIE </em>
<% end %>
This worked for me

I tried it, it works!
<ul>
<% zombies.each do |zombie| %>
<li>
<%= zombie.name %>
<% if zombie.tweets.length > 1 %>
SMART ZOMBIE
<% end %>
</li>
<% end %>
</ul>

Related

Rails view returning database field after loop

In my view I do this:
<ul class="list-group">
<%= JSON.parse(current_printer.stripe_managed_account.fields_needed).each do |f| %>
<li class="list-group-item">
<%= f.gsub!(/[._]/, ' ') %>
</li>
<% end %>
</ul>
It lists everything out like it should, but then at the end it returns the value of current_printer.stripe_managed_account.fields_needed. I'm not sure why it does this, or how to prevent it from doing this.
This is a screenshot:
Replace <%= %>, on your each line by <% %>.
Because <%= #your each %> is same that : <% puts #your each %>.
Try this:
<ul class="list-group">
<% JSON.parse(current_printer.stripe_managed_account.fields_needed).each do |f| %>
<li class="list-group-item">
<%= f.gsub!(/[._]/, ' ') %>
</li>
<% end %>
</ul>
Thanks #vee

how to display an item once from a list where that item shows up multiple times in ruby

I've written the code in my .html.erb file but am thinking it might more sense to write it in my controller? This code returns all the names. I'm trying to figure out how to fetch a tag name only once and if a user inputs a tag name that has already been used it does not show up again.
<ul>
<% #pictures.each do |pic| %>
<% pic.tags.each do |tag| %>
<li>
<%= tag.name %>
</li>
<% end %>
<% end %>
</ul>
<ul>
<% #pictures.collect{|x| x.tags.uniq}.flatten.uniq.each do |tag| %>
<li>
<%= tag.name %>
</li>
<% end %>
</ul>
Using Enumerable#flat_map and Array#uniq:
<ul>
<% #pictures.flat_map { |pic| pic.tags.map(&:name) }.uniq.each do |name| %>
<li>
<%= name %>
</li>
<% end %>
</ul>
Example
<ul>
<% #pictures.each do |pic| %>
<% pic.tags.uniq_by(&:name).each do |tag| %>
<li>
<%= tag.name %>
</li>
<% end %>
<% end %>
Use the Array#- method for all items collected that the user enters.
this is relative to how you store the tag values, inside your save method you should make the tags array to be uniq.

basic counter to increment a link is not displaying properly in my view

I have a number of steps and I have a counter that I would like to increment as the block iterates through the steps. More specifically, if its media type is "excel" or anything else I would like it to say step 2, step 3, step 4 etc. However in the view it only says step 1, step 1, step 1 - it does not properly increment. What is wrong? Any help would be greatly appreciated.
<% #step_list.each do |i| %>
<% x = 0 %>
<% case %>
<% when i.media_type == 'video' %>
<% x += 1 %>
<li class="active">
<span>Video</span>
</a>
</li>
<% when i.media_type == 'excel' %>
<% x += 1 %>
<li class="">
<span>Step <%= x %> </span>
</li>
<% else %>
<% x += 1 %>
<li class="dark-nav ">
<span>Step <%= x %></span>
</li>
<% end %>
<% end %>
The problem in your code is that you initialize the variable x in each loop, you should initialize it to zero before your loop.
To solve this, You can refactor this with the method .each_with_index do |element, i|:
<% #step_list.each_with_index do |step, i| %>
<% case step.media_type %>
<% when 'video' %>
<li class="active">
<span>Video</span>
</li>
<% when 'excel' %>
<li class="">
<span>Step <%= i %></span>
</li>
<% else %>
<li class="dark-nav ">
<span>Step <%= i %></span>
</li>
<% end %>
<% end %>
Why don't you use each_with_index ?
[1, 2, 3].each_with_index do |n, i|
end
You reset your x each time, try that :
<% #step_list.each_with_index do |i,index| %>
<% case i.media_type %>
<% when 'video' %>
<li class="active">
<span>Video</span>
</a>
</li>
<% when 'excel' %>
<li class="">
<span>Step <%= index %> </span>
</li>
<% else %>
<li class="dark-nav ">
<span>Step <%= index %></span>
</li>
<% end %>
<% end %>
<% end %>

Whats wrong with this else if statement?

Basically my controller is just grabbing all members: #members = Member.all and Im looping through them while checking to see if they have a profile picture uploaded and if not then the default should be loaded:
<% #members.each do |member| %>
<% unless member.image.nil? %>
<li style="float:left; width:100px;">
<%= image_tag(member.image.url(:tiny)) %>
<%= link_to member.email, member_path(member) %>
</li>
<% else %>
<li style="float:left; width:100px;">
<%= image_tag("default_member_small.jpg") %>
<%= link_to member.email, member_path(member) %>
</li>
<% end %>
<% end %>
It seems to think every member has a profile image, and the image tag is calling "images/tiny/missing.png" for the missing images.
What gives?
I am guessing you are using paperclip, if you are, you should not use nil?, you should use present?:
<% #members.each do |member| %>
<% if member.image.present? %>
<li style="float:left; width:100px;">
<%= image_tag(member.image.url(:tiny)) %>
<%= link_to member.email, member_path(member) %>
</li>
<% else %>
<li style="float:left; width:100px;">
<%= image_tag("default_member_small.jpg") %>
<%= link_to member.email, member_path(member) %>
</li>
<% end %>
<% end %>
And instead of having an if you should just have this image named as paperclip expects it, there should not have any ifs in your code for this kind of handling.

Rails ancestry treemenu help

I am trying to make a simpel treelistmenu with ancestry.
Here is my code in view and it is not working:
<ul>
<% for cat_opg in CatOpg.roots %>
<li> <%= cat_opg.navn %><li>
<% for cat_opg in CatOpg.children %>
<li> <%= cat_opg.navn %><li>
</ul>
<% end %>
</ul>
<% end %>
And my controller:
def liste
#cat_opg = CatOpg.find(:all)
end
I want to make this simpel tree menu as:
Root
-children
Root
-children
I dont know what i am doing wrong.
PS: I am a rails beginner
First of all, you are getting to model in view, not to local variable.
Second, you're rewriting variable.
It should be something like this:
<ul>
<% cat_opg.roots.each do |cat_opg_root| %>
<li> <%= cat_opg_root.navn %><li>
<% cat_opg_root.children each do |cat_opg_child| %>
<li> <%= cat_opg_child.navn %><li>
</ul>
<% end %>
</ul>
<% end %>
Alex thank you for your answer.
Now it works
Controller: #cat_opg = CatOpg
And the view:
<ul>
<% #cat_opg.roots.each do |cat_opg_root| %>
<li> <%= cat_opg_root.navn %></li>
<% unless cat_opg_root.children.empty? %>
<ul>
<% cat_opg_root.children.each do |cat_opg_child| %>
<li> <%= cat_opg_child.navn %></li>
<% end %>
</ul>
<% end %>
<% end %>
</ul>

Resources