how not to loop twice on the same items - RUBY - ruby-on-rails

I have this loop :
<div>
<% #ingredientsOfRecipes.each do |item| %>
<ul>
<%= item["name"] %>
<%#fridge.each do |f|%>
<% item['ingredients'].each do |ingredient| %>
<li style="display: flex; flex-drirection: row; align-items: center">
<% if ingredient.include? f.content%>
<p style="color: green">
<%=ingredient%>
<i class="fa-solid fa-check" style="color: green; margin-left: 10px"></i>
</p>
<% elsif ingredient.exclude? f.content%>
<p style="color: red">
<%=ingredient%>
<i class="fa-solid fa-xmark" style="color: red; margin-left: 10px"></i>
</p>
<%end%>
</li>
<% end %>
<%end%>
</ul>
<% end %>
On the #fridge variables, I have 4 values.
and once I loop on a particular ingredient, I don't want to loop on it anymore.

There are some things that are not clear for me in your code, but right now I can't do questions, so I hope you can correct me if something is not working. Why you have elseif instead of only else?
If you want to stop the loop, simple use the break word wherever you need:
<% #ingredientsOfRecipes.each do |item| %>
<ul>
<%= item["name"] %>
<% #fridge.each do |f|%>
<% item['ingredients'].each do |ingredient| %>
<li style="display: flex; flex-drirection: row; align-items: center">
<% break if ingredient.include? 'my_ingredient_that_should break_the_loop'%>
<% if ingredient.include? f.content %>
<p style="color: green">
<%=ingredient%>
<i class="fa-solid fa-check" style="color: green; margin-left: 10px"></i>
</p>
<% elsif ingredient.exclude? f.content%>
<p style="color: red">
<%=ingredient%>
<i class="fa-solid fa-xmark" style="color: red; margin-left: 10px"></i>
</p>
<%end%>
</li>
<% end %>
<%end%>
</ul>
<% end %>
And trying to do your code more clear: supposing #fridge is a collection of objects and item['ingredients'] is an array of strings (so ingredient is a String) you can try something like this:
<% #ingredientsOfRecipes.each do |item| %>
<ul>
<%= item["name"] %>
<% item['ingredients'].each do |ingredient| %>
<li style="display: flex; flex-drirection: row; align-items: center">
<% break if ingredient.include? 'my_ingredient_that_should break_the_loop'%>
<% if #fridge.collect(&:content).any? {|c| ingredient.include? c } %>
<p style="color: green">
<%= ingredient %>
<i class="fa-solid fa-check" style="color: green; margin-left: 10px"></i>
</p>
<% else %>
<p style="color: red">
<%= ingredient %>
<i class="fa-solid fa-xmark" style="color: red; margin-left: 10px"></i>
</p>
<%end%>
</li>
<%end%>
</ul>
<% end %>
Is that your question?

Related

Index page is not re rendering/refreshing on search in Rails

I am making a simple search in rails on the index page. My problem is that on debug I get the correct results but they don't show on the frontend.
def index
if !params[:category].present? and !params[:title].present?
#services = Service.take(4)
else
if params[:category] != "Find Service" and params[:title].present?
#services = Service.where(category: params[:category], title: params[:title]).take(4)
elsif params[:category] == "Find Service" and params[:title].present?
#services = Service.where(title: params[:title]).take(4)
elsif params[:category] != "Find Service" and !params[:title].present?
#services = Service.where(category: params[:category]).take(4)
else
#services = Service.take(4)
end
end
end
My view index.html.erb
<%= form_with(url: "services", method: "get") do |form| %>
<div class="banner-input" style="margin-top">
<div class="listing-sort listing-sort--main-listing" style="width: 50%;float: left; border-radius: 6px; border: 2px solid white; background: transparent; padding: 0px;">
<input type="submit" value="" class="main-listing__form-field agent-submit" style="width: 10% ;float:right">
<%= form.text_field "title", id: "search_agent", class: "main-listing__form-field ", placeholder: "Search by Name or Location...", style: "width: 75% ;float:right; background: transparent; color: white!;" %>
<div class="listing-sort__inner" style="float:left;margin: 0px; width: 25%; padding: 0px;">
<p class="listing-sort__sort">
<%= form.select("category",options_for_select(["Find Service", "Assembly", "Carpet Cleaning", "Electrical", "House Cleaning", "HVAC", "Handyman", "IT", "Junk Removal", "Lawn Care","Moving", "Painting", "Plumbing", "Staging", "Photography", "Videography", "Inspectors"], :selected => "Find Service"), {include_blank: false}, { :class => 'ht-field listing-sort__field' }) %>
</p><!-- .listing-sort__sort -->
</div><!-- .listing-sort__inner -->
</div><!-- .listing-sort -->
</div>
<% end %>
<section style="margin-top: 50px">
<h2 style="margin-left: 15px; padding-bottom: 30px; font-size: 24px;">Recently viewed & more</h2>
<% #services.each do |service| %>
<%= render 'services/service_card', :f => service %>
<% end %>
</section>
<section class="jumbotron"style="margin-top: 50px">
<h2 style="margin-left: 15px; padding-bottom: 30px; font-size: 24px;">Pro SEM services
</h2>
<% #services.each do |service| %>
<%= render 'services/service_card', :f => service %>
<% end %>
</section>
This is _service_card
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-3" style="padding-bottom: 10px">
<div class="boxes">
<div class="box-img">
<img src="assets/MobileApps.png">
</div>
<div class="user-detail-main">
<div class="user-detail-img">
<img src="assets/MobileApps.png">
</div>
<div class="user-detail">
<a><h3><%= f.user.profile.first.first_name %></h3></a>
<p>Level 1 Seller</p>
</div>
<div class="user-detail-para">
<% byebug %>
<p><%= f.description %></p>
</div>
<div style="padding-top: 50px;">
<i class="fas fa-star" style="color: #ffbf00"></i> (5)
</div>
</div>
<div class="user-detail-footer">
<div class="heart-icon">
<i class="fas fa-heart" style="color: #c6c6c6"></i>
<i class="fas fa-bars" style="color: #c6c6c6"></i>
</div>
<div class="user-value">
<p>Starting At $ <%= f.packages.first.price%></p>
</div>
</div>
</div>
Please help me regarding this. Looking forward. It is working fine without search, but with search it doesn't update the results and show same results.

Rails - Group_by method doesn't work

i'm trying to group items (games) by month in rails and SQL3 but it generates this :
Error
March 17th
<% #game_months.each do |month, games| %>
<% for game in games %>
<a href="<%= game_path(game)%>" class="col-md-4 m-t-md">
<div class="box b-a m-a" style="box-shadow: 0px 4px 2px 0px rgba(0,0,0,0.02);">
Here is my games controller
class GamesController < ApplicationController
def index
#game = current_user.games
#games = Game.all.order("created_at DESC")
#game_months = #games.group_by {|g| g.created_at.beginning_of_month }
end
My view
<% #game_months.each do |month, games| %>
<% for game in games %>
<a href="<%= game_path(game)%>" class="col-md-4 m-t-md">
<div class="box b-a m-a" style="box-shadow: 0px 4px 2px 0px rgba(0,0,0,0.02);">
<div class="item b-b">
<div><%= image_tag game.photos[0].image.url(:medium), class:"img-responsive" if game.photos.length > 0 %></div>
</div>
<div class="p-a text-left white">
<h6 class="text-md text-black block p-b-sm"><strong><%= game.game_name %></strong></h6>
<span class="text-muted p-t-md"><%= game.game_description %></span>
<div class="m-t-md">
<span><%= image_tag avatar_url(game.user), class:"w-24 circle b-a" %></span>
<span class="m-t-sm text-black pull-right text-md"></span>
</div>
</div>
</div>
</a>
<% end %>
<% end %>
I don't know what i'm doing bad. Please help, i don't know what to do to solve this!
CHEERS! :)
Change
#games = Game.all.order("created_at DESC")
#game_months = #games.group_by {|g| g.created_at.beginning_of_month }
To
#game_months = Game.all.group_by { |g| g.created_at.month }
It should produce hash looking like this:
{1=>[GameObject1], 2=>[GameObject3, GameObject4], 3=>[GameObject2] ...}
Now iterate over hash and do what you want to do.

Random text after rails loop

I created a loop to show all blog posts. I am getting an error that displays random text after the loop:
It looks like there's a stack of texts with all the post information listed at the end of the loop, I have no idea how it happened and how to fix it.
I checked my HTML and CSS, but couldn't figure out what caused the issue.
Below is the HTML and CSS code:
<section id="posts" class="wrapper">
<h2>My Latest Articles</h2>
<hr>
<div class="post_container">
<%= #posts.each do |p| %>
<div class="article">
<h3 class="post_title"><%= link_to p.title, p %></h3>
<p class="post_date"><%= p.created_at.strftime("%A,%b %d") %></p>
<p class="content"><%= truncate(p.content, length: 400) %></p>
</div>
<% end %>
</div>
<div class="button_wrapper">
More Articles
</div>
</section>
CSS
#posts {
padding: 6.5em 0 10em 0;
h2 {
text-align: center;
color: $text;
margin-bottom: 1.25rem;
}
.post_container {
padding: 6em 0 6em 0;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: flex-start;
.article {
max-width: 28%;
}
.post_title {
color: $text;
font-size: 1.6em;
}
.post_date {
padding: .75rem 0;
color: $accent;
font-size: 1.2em;
}
.content {
color: $grey;
}
}
}
<%= #posts.each do |p| %>
Should be
<% #posts.each do |p| %>
Do not output the result of .each.
Its because your using <%= at the beginning of your loop. You would use <% instead
Change the post_container div to this:
<div class="post_container">
<% #posts.each do |p| %>
<div class="article">
<h3 class="post_title"><%= link_to p.title, p %></h3>
<p class="post_date"><%= p.created_at.strftime("%A,%b %d") %></p>
<p class="content"><%= truncate(p.content, length: 400) %></p>
</div>
<% end %>
</div>
Use
<% #posts.each do |p| %>
because <= %> use for print the value in your case it'll print the whole loop

Rails if greater than 0

I just started playing with rails, what's the best way to do the following: #events.x is an integer, if its value isn't > 0 it has to be hidden.
<div class="chartwell radar">
<span style="color: #fff;">dx</span>
<span style="color: #3498db"><%= #events.design %></span>
<span style="color: #1abc9c"><%= #events.typography %></span>
<span style="color: #2c3e50"><%= #events.code %></span>
<span style="color: #f8ff08"><%= #events.art %></span>
<span style="color: #fd79f2"><%= #events.lifestyle %></span>
</div>
As per your comment you want to show the value only if the value of the integer is no 0. so you can use this example:
<span style="color: #3498db"><%= #events.design unless #events.design.to_i == 0 %></span>
Or if you want not to display the span too
<% unless #events.design.to_i == 0 %>
<span style="color: #3498db"><%= #events.design %></span>
<% end %>
Let me know if you meant something different.
When you say "it is hidden", what is "it"? The whole div above? Something else?
If you wanted to conditionally show a div for #events.x, like #events.art for example, you could do
<% unless #events.art == 0 %>
<span style="color: #f8ff08"><%= #events.art %></span>
<% end %>
doing this for all of them is a bit tedious and repetitive. You could dry it up like this:
<div class="chartwell radar">
<span style="color: #fff;">dx</span>
<%
[["#3498db", "design"],
["#1abc9c", "typography"],
["#2c3e50", "code"],
["#f8ff08", "art"],
["#fd79f2", "lifestyle"]].each do |color, method| %>
<% value = #events.send(method) %>
<% unless value == 0 %>
<span style="color: <%= color %>;"><%= value %></span>
<% end %>
<% end %>
</div>

Tree menu using ancestry in rails

according to this answer https://stackoverflow.com/a/11335283/2397494
i'm trying to make tree menu for my appliction
this answer working only for root and first children.
I'm looking for suggestion to transform this code, work well with n childrens.
<ul id="menu">
<% Hub.roots.each do |category| %>
<li> <%= link_to h(category.title), category %>
<% unless category.children.empty? %>
<ul id="sub-menu">
<% category.children.each do |subcategory| %>
<li><%= link_to h(subcategory.title), subcategory %></li>
<% end %>
</ul>
<% end %>
</li>
<% end %>
</ul>
<% Hub.roots.each do |category| %>
<div> <%= link_to h(category.title), category %>
<% if category.children.present? %>
<div id="submenu">
<%= render 'hubs/sub', category: category %>
</div>
<% end %>
</div>
<% end %>
partial _sub
<ul>
<% category.children.each do |sub| %>
<li>
<%= link_to_unless_current sub.title, sub %>
<%= render 'hubs/sub', category: sub %>
</li>
<% end %>
</ul>
and css
#submenu > ul, ol {
margin-right: 20px;
li {
font-size: 11px;
margin: 4px 0;
margin-bottom:-5px !important;
}
ul, ol {
font-size: 11px;
margin: 0;
padding-left: 16px;
margin-bottom:-5px !important;
li {
margin: 4px 0;
}
}
}

Resources