relation using orm rails two tables - ruby-on-rails

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

Related

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.

Accessing list of projects from accounts

Below is the code which gives me list of accounts.
<% #accounts.each do |account| %>
<li><%= link_to account.name %></li>
<% end %>
How to access the list of projects of my specific account.
Try this:
<% #accounts.each do |account| %>
<li><%= link_to account.name %></li>
<li><%= link_to account.projects %></li> #this will give you a collection of projects associated with that account
<% end %>
If you want a link_to for each individual project then you'll have to use another loop like this:
<% #accounts.each do |account| %>
<li><%= link_to account.name %></li>
<% account.projects.each do |project| %>
<li><%= link_to project %></li> #this will give you individual project associated with that account
<% end %>
<% end %>
Edit:
Incase you don't have any projects for an account you can do:
<% #accounts.each do |account| %>
<li><%= link_to account.name %></li>
<% if account.projects %>
<% account.projects.each do |project| %>
<li><%= link_to project %></li> #this will give you individual project associated with that account
<% end %>
<% else %> # add this else block to execute your code when there are on projects
<p> No projects associated with your account</p>
<% end %>
<% 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