Rails ancestry treemenu help - ruby-on-rails

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>

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

Sorting in html.erb

This code grabs information from yaml files and outputs them in ascending order (I'm assuming by default). I can't figure out how to code this to reverse the order. Any suggestions? The site is using Middleman and ERB as templating language.
View:
<ul class="app-list">
<% data.apps.each do |app| %>
<% if app[1][:categories][:featured] && app[1][:categories][:script] %>
<li>
<%= partial 'partials/app', locals: { app: app[1] } %>
</li>
<% end %>
<% end %>
</ul>
Try:
<ul class="app-list">
<% data.apps.reverse.each do |app| %>
<% if app[1][:categories][:featured] && app[1][:categories][:script] %>
<li>
<%= partial 'partials/app', locals: { app: app[1] } %>
</li>
<% end %>
<% end %>
</ul>

How to remove the end of the "s" in partial "_error_messages"

How to remove the end of the "s" in partial "_error_messages".
<% if event.errors.any? %>
<div id="errorExplanation">
<h2> В форме обнаружено <%= pluralize(event.errors.count, "ошибки") %>:</h2>
<ul>
<% event.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
You need to do...
pluralize(event.errors.count, "ошибка", "ошибки")
It will use the singular term if one, and the plural term otherwise.

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.

if statement in 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>

Resources