print html entity in rails view using times method - ruby-on-rails

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>

Related

How to handle a Rails query causing massive delays

So I'm using the query below that is then being used as a submenu and it's causing significant loading issues. I've put it in the application_helper for the time being.
def category_level
Connector::Category.includes(:products).group_by(&:parent_category_id)
end
Then it's being displayed on the home page with the following:
<ul class="list-style-upper">
<% category_level[nil].each do |root| %>
<%= render 'products/submenu/category_item', category: root %>
<% end %>
</ul>
Which redirects to
<li class="list-one">
<%= category.name %><p><%= category.products.count %></p>
<% if category_level[category.id].present? %>
<ul class="list-style-upper-sub">
<% category_level[category.id].each do |child| %>
<%= render 'products/submenu/category_item', category: child %>
<% end %>
</ul>
<% end %>
</li>
The issue is that there are typically around 30,000 products. Is there a way to build out the results and store them for display?

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 iterate through my array within a hash

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>

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.

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