How to iterate through my array within a hash - ruby-on-rails

Firstly, I am hoping my understanding is correct and I do in fact have a an array within my hash.
I've gotten so far but cant seem to iterate through each hash and its array. What I would like to do is have each tournament grouped by date (year only) and have all its tournaments listed underneath.
So far my logic is
Controller
#tournaments = Tournament.all
#tournament_year = #tournaments.group_by {|y| y.tourn_date.year}
View
<ul class="resp-tabs-list">
<% #tournament_year.each do |y, t| %>
<li><%= y %></li>
<% end %>
</ul>
hash
{2014=>[#<Tournament id: 3, name: "WTS ", tourn_date: "2014-04-26", tourn_location: "Cardiff", created_at: "2014-04-26 14:57:21", updated_at: "2014-04-26 14:57:21">, #<Tournament id: 4, name: "CTS Nottingham", tourn_date: "2014-05-26", tourn_location: "Nottingham", created_at: "2014-04-26 14:57:39", updated_at: "2014-04-26 14:57:48">]}
My desired output would be below
<h3>2014</h3>
<ul>
<li>Tournament Name 1</li>
<li>Tournament Name 2</li>
</ul>
<h3>2013</h3>
<ul>
<li>Tournament Name 1</li>
<li>Tournament Name 2</li>
</ul>

Change your view as below:
<ul class="resp-tabs-list">
<% #tournament_year.each do |y, t| %>
<h3><%= y %></h3>
<ul>
<% t.each_with_index do |tournament, i| %>
<li><%= tournament.name %> <%= i %></li>
<% end %>
</ul>
<% end %>
</ul>

I would suggest
<% #tournament_year.each do |y, t| %>
<h3> <%= y %></h3>
<ul class="resp-tabs-list">
<% t.each do |tournament| %>
<li><%= "#{tournament.to_s}" %></li>
<% end %>
</ul>
<% end %>
where
#app/models/tournament.rb
def to_s
"#{name} #{id}"
end

Ive just come up with this solution as an example, seems to work, can anyone see any reason why this would be incorrect?
<ul class="resp-tabs-list">
<%= #tournament_year.each do |y, t| %>
<li><%= y %></li>
<%= #tournament_year[y].each do |x| %>
<li><%= x.name %></li>
<% end %>
<% end %>
</ul>

Related

Displaying post base on category in Rails

How can I display the post base in the current category, I can show the category but only one post show.
In my controller:
#area_category = Project.joins(:area_category).group(:area_category_id)
In my view
<% #area_category.each do |category| %>
<%= category.area_category_name %>
<% category.project_title %>
<% end %>
Basically, i want to display category and post on each category
Something like this:
<ul>
<li>Category 1
<ul>
<li>Post 1</li>
<li>Post 2</li>
</ul>
</li>
<li>Category 2
<ul>
<li>Post 1</li>
<li>Post 2</li>
</ul>
</li>
</ul>
controller
#area_category = AreaCategory.preload(:projects)
view
<% #area_category.each do |category| %>
<%= category.area_category_name %>
<% category.projects.each do |project| %>
<%= project.project_title %>
<% end %>
<% end %>

relation using orm rails two tables

I need to print relations Emissions With trades.
Example:
-Trade 1
Emission 1
Emission 2
Actuallly my code print.
-Trade 1
Emission 1
-Trade 1
Emission 2
This code:
<% #emissions.group_by(&:trade).each do |trade, emission| %>
<% emission.each do |e| %>
<% if (e.users.present?) %>
<li><%= trade.name %></li>
<ul>
<li><%= e.name %></li>
</ul>
<% end %>
<% end %>
</li>
<% end %>
UPDATE
The list should appear if the user relationship is associated with the emission.
For instance:
<% If (e.users.present?)%>
<% = emission.name%>
This is only displayed if the emission ratio with the user exists.
The company name should appear if this relationship exists.
Try the following
<% If (emission.users.present?)%>
<% = trade.name%>
<% = emission.name%>
But this does nothing but repeat twice for each issue the company name.
Your indentation is all over the place and you have lots of html tags which have an <% end %> in the middle, and stuff like that, so it's a bit confusing, but i think that you're trying to do something like this:
<% #emissions.group_by(&:trade).each do |trade, grouped_emissions| %>
<li><%= trade.name %>
<ul>
<% grouped_emissions.each do |emission| %>
<% if (emission.users.present?) %>
<li><%= e.name %></li>
<% end %>
<% end %>
</ul>
</li>
<% end %>

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.

print html entity in rails view using times method

I am trying to print Rating for bookmarks in rails view. I am trying to take out the rating ("3") from a hash and then convert that string to an int and then print solid star (html entity) that many times. Here is what I have been trying but it just prints the number. I am very new to ruby-on-rails, so I might be missing something very trivial here.
<% #bookmarks.each_with_index do |bookmark, index| %>
<div class="bookmark">
<%= link_to bookmark_path(index) do %>
<ul>
<li><%= bookmark[:type] %></li>
<li><%= bookmark[:title] %></li>
<li>
<%= bookmark[:rating].to_i.times do print "&bigstar;" end %>
<%= bookmark[:rating].to_i.times do print "&bigstar;" end %>
</li>
</ul>
<% end %>
</div>
<% end %>
I think I have figured it out by myself.
<li>
<% bookmark[:rating].to_i.times do %>
&bigstar;
<% end %>
</li>

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