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>
Related
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?
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.
I used Ruby on Rails and I have created more of 40 hiraganas flashcards. In my index view, I do an iteration like this.
Some of Hiraganas cards have different I want to create different columns which host different color cards.
I want to iterate on letter A = green, E = red, I = Pink, etc
enter image description here
I only do that
<% #hiraganas.each do |hiragana| %>
<li>
<%= render 'allhiraganas', hiragana: hiragana %>
</li>
<% end %>
Do you know how to select only few elements to iterate. I see some tutos but I try .select
here is the render flashcards code
<div class="row">
<ul class="list-inline text-center card-frame">
<li>
<div class="card">
<div class="front">
<% if current_user.try(:admin?) %>
<%= link_to hiragana_path(hiragana), class:'trash-hiragana', data: { confirm: 'Are you sure?' }, method: :delete do %>
<%= image_tag("delete-btn.png") %>
<% end %>
<% end %>
<!-- PARAMETRER LE RESPONSIVE BOOTSTRAP -->
<!-- <div class="col-sm- col-xs-4 col-md-3"> -->
<span class="card-question popover-word" data-content="<h4 class='text-center letter-uppercase'><%= hiragana.bigletter.upcase %></h4><p class='text-center'><b><%= hiragana.midletter %></b> comme dans <b><%= hiragana.transcription %></b></p>">
<i class="fa fa-eye fa-lg"></i>
</span>
<!-- son de l'hiragana -->
<span class="audioclick popover-word" data-content="<p class='text-center'><b>le son arrive prochainement !</b></p>">
<i class="fa fa-volume-up fa-lg"></i>
</span>
<!-- image mnémotechnique -->
<span class="idea popover-word" data-content="<p class='text-center'><b>l'image est bientôt prête !</b><%= hiragana.upload %></p>">
<i class="fa fa-lightbulb-o fa-lg"></i>
</span>
<!-- <div class="prononciation">
<i class="fa fa-comment"></i>
</div> -->
<div class="card-hiragana hiragana-<%=hiragana.bigletter.downcase.last%>">
<h1><b><%= hiragana.ideo1 %></b></h1>
</div>
<div class="card-katakana">
<p><%= hiragana.ideo2 %></p>
</div>
<div id="favorite_<%=hiragana.id%>">
<%= render 'favs/favorites', hiragana: hiragana %>
</div>
</div>
<div class="back">
<div class="col-sm-3 col-xs-4 col-md-3 containerbackcards-<%=hiragana.bigletter.downcase.last%>">
<div class="backcard-hiragana">
<h1><b><%= hiragana.ideo1 %></b></h1>
</div>
<div class="card-bigletter">
<h4><%= hiragana.bigletter.upcase %></h4>
</div>
</div>
</div>
</div>
</li>
</ul>
</div>
Thank you for your answer
which ones do you want? what's the logic for which you'd like to choose? If you simply wanted the first 3 you could say
#hiriganas = Hirigana.first(3)
if you have a condition you'd like to choose them by select is a good choice.
#hiriganas = #hiriganas.select{ |hir| # criteria for selection }
It depends: If you have the color saved in the hiragana model, then you just need to:
greenhiriganas = Hirigana.where(color: "green")
If you just know in your head, which letters have which colors, then for each section you need to write an array containing all the names
#if for example A, Ka and Shi are green
green = [:a, :ka, :shi]
#then select via include?-method
greenhiriganas = Hirigana.select{ |hiri| green.include?(hiri.name) }
I would like to display an agenda that shows who is available for roster for the next seven days, with the following logic:
Looping through each of the 7 days, for each day:
If the leave starts today, show Starts %H:%M
If the leave finishes today, show Finishes %H:%M
If the leave starts and finishes today, show %H:%M - %H:%M
If the leave doesn't start or finish today but spans over today, show all day
Should include leave that start or finish outside of the 7 days but span the 7 days being displayed.
I am hoping for someone to point me in the right direction preferably using Rails and Postgres. I am not against doing the queries for each day since at most there will only be 30 days displayed, but also looking for reasonably performant code since there could be 100+ leave records per day.
First example that does not fulfill the criteria outlined above.
<% #dates.each do |d| %>
<h5><%= d.strftime("%A %d %b") %></h5>
<% #leave = Leave.where('(start_at BETWEEN ? AND ?) OR (end_at BETWEEN ? AND ?)', d.beginning_of_day, d.end_of_day, d.beginning_of_day, d.end_of_day) %>
<% #leave = #leave.where.not('end_at < ?', Time.current) %>
<% if #leave.any? %>
<ul>
<% #leave.each do |leave| %>
<% if leave.single_day? && leave.start_at.to_date == d %>
<li>
<label class="label label-danger"><%= leave.start_at.strftime('%H:%M') %> - <%= leave.end_at.strftime('%H:%M') %></label>
<%= leave.user.name %>
</li>
<% elsif !leave.single_day? %>
<% if leave.start_at.to_date == d %>
<li>
<label class="label label-danger">From <%= leave.start_at.strftime('%H:%M') %></label>
<%= leave.user.name %>
</li>
<% elsif leave.end_at.to_date == d %>
<li>
<label class="label label-danger">Until <%= leave.end_at.strftime('%H:%M') %></label>
<%= leave.user.name %>
</li>
<% else %>
<li>
<label class="label label-danger">All Day</label>
<%= leave.user.name %>
</li>
<% end %>
<% end %>
<% end %>
</ul>
<br/>
<span class="label label-success" style="margin-right: 10px;">
<span class="glyphicon glyphicon-ok"></span>
</span>
<%= current_account.users.active.where.not(id: #leave.map(&:user_id)).count %> Available
<% else %>
<span class="label label-success" style="margin-right: 10px;">
<span class="glyphicon glyphicon-ok"></span>
</span>
<%= current_account.users.active.count %> Available
<% end %>
<br />
<br />
<% end %>
I would start with something like this:
In the model:
def starts_on?(date)
start_at.to_date == date
end
def ends_on?(date)
end_at.to_date = date
end
def covers?(date)
leave.start_at.to_date <= date && leave.end_at.to_date >= date
end
def starts_and_ends_on?(date)
starts_on?(date) && ends_on?(date)
end
In the controller:
#dates = # define the date range like you already do, I assume it is an array
#leaves = Leave.includes(:user).
where('start_at =< ? AND end_at >= ?', dates.max, dates.min)
In the helper:
def relevant_leaves_for_date(date, leaves)
leaves.select { |leave| leave.covers?(date) }
end
def leave_description_for_date(leave, date)
if leave.starts_and_ends_on?(date)
"#{leave.start_at.strftime('%H:%M')} - #{leave.end_at.strftime('%H:%M')}"
elsif leave.starts_on?(date)
"From #{leave.start_at.strftime('%H:%M')}"
elsif leave.ends_on?(date)
"Until #{leave.end_at.strftime('%H:%M')}"
else
'All day'
end
end
def available_users(leaves)
current_account.users.active.count - leaves.size
end
In the view:
<% #dates.each do |date| %>
<h5><%= date.strftime("%A %d %b") %></h5>
<% leaves = relevant_leaves_for_date(date, #leaves) %>
<% if leaves.any? %>
<ul>
<% leaves.each do |leave| %>
<li>
<label class="label label-danger">
<%= leave_description_for_date(leave, date) %>
</label>
<%= leave.user.name %>
</li>
<% end %>
</ul>
<% end %>
<span class="label label-success">
<span class="glyphicon glyphicon-ok"></span>
</span>
<%= available_users(leaves) %> Available
<% end %>
You might notice that I remove the <br> and style tags from the html. Please do not use <br> for styling or style attributes in the html. Add class the the tags and style them in your css file.
The reason why case 4 & 5 are not working is you are not fetching the leaves which started before the beginning of day or which are going to end after the end of day. So you can fetch all leaves which have not ended yet.
<% #leave = Leave.where('(end_at >= ?', d.beginning_of_day) %>
And then in application_helper.rb, you can have a function like this,
def check_for_leave(leave,d)
msg = ""
if leave.single_day? && leave.start_at.to_date == d
msg = "#{leave.start_at.strftime('%H:%M')} - #{leave.end_at.strftime('%H:%M')}"
elsif !leave.single_day?
if leave.start_at.to_date == d
msg = "From #{leave.start_at.strftime('%H:%M')}"
elsif leave.end_at.to_date == d
msg = "Until #{leave.end_at.strftime('%H:%M')}"
else
msg = "All Day"
end
return msg
end
In html.erb file
<% #dates.each do |d| %>
<h5><%= d.strftime("%A %d %b") %></h5>
<% #leave = Leave.where('(end_at >= ?', d.beginning_of_day) %>
<% if #leave.any? %>
<ul>
<% #leave.each do |leave| %>
<li>
<label class="label label-danger">
<%= check_for_leave(leave, d) %>
</label>
<%= leave.user.name %>
</li>
<% end %>
</ul>
<br/>
<span class="label label-success" style="margin-right: 10px;">
<span class="glyphicon glyphicon-ok"></span>
</span>
<%= current_account.users.active.where.not(id: #leave.map(&:user_id)).count %> Available
<% else %>
<span class="label label-success" style="margin-right: 10px;">
<span class="glyphicon glyphicon-ok"></span>
</span>
<%= current_account.users.active.count %> Available
<% end %>
<br />
<br />
<% end %>
For example I have this file:
_goal.html.erb
<table>
<tr>
<%= goal.name %>
<span class="label label-info"><%= goal.deadline.strftime("%d %b %Y") %></span>
</tr>
</table>
It is being rendered from the home page:
<h1><b>Goals</b></h1>
<%= render #unaccomplished_goals %>
<%= render #accomplished_goals %>
<% end %>
How can we wrap an accomplished goal with <span class="label label-info-success"> but if it is an unaccomplished goal keep it <span class="label label-info">?
In the controller:
#accomplished_goals = current_user.goals.accomplished
#unaccomplished_goals = current_user.goals.unaccomplished
Here was my latest attempt, which just made them all -info:
<% if #unaccomplished_goals == true %>
<span class="label label-info"><%= goal.deadline.strftime("%d %b %Y") %></span>
<% else #accomplished_goals == true %>
<span class="label label-warning"><%= goal.deadline.strftime("%d %b %Y") %></span>
<% end %>
Maybe you'll have more luck :)
Thank you so much.
Create a helper method that returns the correct classes for the goal's status. In application_helper.rb:
def deadline_label_class(goal)
if goal.accomplished?
'label label-info'
else
'label label-warning'
end
end
This assumes that Goal has an instance method called accomplished? which returns true/false. You may have to write that method if it doesn't exist or use some other criteria.
Then use the helper in the _goal.html.erb template:
<table>
<tr>
<%= goal.name %>
<span class="<%= deadline_label_class(goal) %>">
<%= goal.deadline.strftime("%d %b %Y") %>
</span>
</tr>
</table>