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.
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 am figuring out why it showing duplicate data on the front page:
All I did is to use Rails collection rendering.
Here's my application.html.erb file:
<body>
<%= render 'layouts/navbar' %>
<!-- content -->
<main class="pt-5">
<div class="container">
<div class="row">
<%= render 'layouts/sidebar' %>
<%= yield %>
<%= render 'layouts/footer' %>
</div>
</main>
</body>
</html>
Here's my index.html.erb file:
<div class="col-md-9">
<% if #contacts.blank? %>
<div class="alert alert-warning">
<strong>Record is empty!</strong>
</div>
<% else %>
<div class="card">
<div class="card-header"><strong>All Contacts</strong></div>
<table class="table">
<%= render #contacts %>
</table>
</div>
</div>
<% end %>
and here's my _contact.html.erb file:
<% #contacts.each do |contact| %>
<tr>
<td class="middle">
<div class="media">
<div class="media-left">
<a href="#">
<%= image_tag contact.gravatar, class: "media-object" %>
</a>
</div>
<div class="media-body">
<h4 class="media-heading"><%= contact.name %></h4>
<address>
<strong><%= contact.address %>, <%= contact.city %>, <%= contact.state %>, <%= contact.zip %></strong><br>
<%= contact.email %> | <%= contact.phone %> | <%= contact.mobile %>
</address>
</div>
</div>
</td>
<td width="100" class="middle">
<div>
<a href="#" class="btn btn-outline-secondary btn-circle btn-xs" title="Edit">
<i class="fa fa-edit"></i>
</a>
<a href="#" class="btn btn-outline-danger btn-circle btn-xs" title="Delete">
<i class="fa fa-times"></i>
</a>
</div>
</td>
</tr>
<% end %>
BTW, I am using Kaminari Gem for pagination. It works however the data shows twice.
Any idea?
UPDATES:
Here's my contacts controller:
class ContactsController < ApplicationController
def index
#contacts = Contact.page params[:page]
end
end
Here's the kaminari config file:
# frozen_string_literal: true
Kaminari.configure do |config|
config.default_per_page = 2
# config.max_per_page = 4
# config.window = 4
# config.outer_window = 0
# config.left = 0
# config.right = 0
# config.page_method_name = :page
# config.param_name = :page
# config.params_on_first_page = false
end
When you do <%= render #contacts %>, Rails will loop results from #contacts and render each one using _contact.html.erb, while embedding local variable contact in each one. You see contacts twice, because you loop twice: remove <% #contacts.each do |contact| %> from _contact.html.erb and it should work.
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.
I'm new to Rails so my code is probably ugly..
I have a loop which give me the 3 latest articles on my index page.
I added a condition to say "if there is no image in my article, give me a image from unsplash".
The issue is : that's apply the condition on the 3 images even if 2 of 3 images exists. I want that only the image which is nil get the unsplash image.
How can I do that ?
<% #articles.last(3).in_groups_of(3, false).each do |group| %>
<div class="row">
<% group.each do |article| %>
<% if article.image.exists? %>
<div class="col-xs-12 col-sm-4">
<div class="card">
<style media>
.card { background: linear-gradient(-225deg, rgba(30,30,30,0.6) 30%, rgba(46,46,46,0.5) 80%), url("<%= article.image.url(:medium) %>"); }
</style>
<img src="<%= article.user.image.url(:thumb) %>" alt="" class="avatar-small card-user">
<div class="card-description">
<h3><%= article.title %></h3>
<p><%= truncate(article.subtitle, length: 45, escape: false) %></p>
</div>
</div>
</div>
<% else %>
<div class="col-xs-12 col-sm-4">
<div class="card">
<style media>
.card { background: linear-gradient(-225deg, rgba(30,30,30,0.6) 30%, rgba(46,46,46,0.5) 80%), url("http://unsplash.it/1280/500/?random"); }
</style>
<img src="<%= article.user.image.url(:thumb) %>" alt="" class="avatar-small card-user">
<div class="card-description">
<h3><%= article.title %></h3>
<p><%= truncate(article.subtitle, length: 45, escape: false) %></p>
</div>
</div>
</div>
<% end %>
<% end %>
</div>
<% end %>
my controller :
class PagesController < ApplicationController
def index
#articles = Article.last(3)
end
end
Thanks a lot for your help !
I finally find the error.
It was <style media> which was the issue.
I finally do this :
<% #articles.last(3).in_groups_of(3, false).each do |group| %>
<div class="row">
<% group.each do |article| %>
<div class="col-xs-12 col-sm-4">
<a href="<%= articles_path(article.slug) %>"><div style="background: linear-gradient(-225deg, rgba(30,30,30,0.6) 30%, rgba(46,46,46,0.5) 80%), url('<%= article.image.url(:thumb) %>');" class="card">
<img src="<%= article.user.image.url(:thumb) %>" alt="" class="avatar-small card-user">
<div class="card-description">
<h3><%= article.title %></h3>
<p><%= truncate(article.soustitre, length: 45, escape: false) %></p>
</div>
</div>
</div></a>
<% end %>
</div>
<% end %>
Now the 3 latest articles are displayed on my index page.
I add a default image with paperclip and withdrawed the condition if else.
I started developing my site in rails 4 with basically the database that comes with RoR sqlt3 and i want to take my site to production in a digital oceans server but they use postgres.. now I thought this was going to work the same way i have i started it. but now im getting errors
(Error)
PG::DatatypeMismatch: ERROR: argument of WHERE must be type boolean, not type timestamp without time zone LINE 1: SELECT "videos".* FROM "videos" WHERE (created_at ) ORDER B... ^ : SELECT "videos".* FROM "videos" WHERE (created_at ) ORDER BY cached_votes_up DESC, created_at DESC
(Controller)
def index
#videos = Video.all.order("cached_votes_up DESC, created_at DESC")
#items = Video.order("cached_votes_up DESC, created_at DESC").where("created_at ", Time.zone.now.beginning_of_day)
#items_by_day = #items.group_by { |t| t.created_at.beginning_of_day }
#topvideo = Video.order("cached_votes_up DESC, created_at DESC").where("created_at >= ?", Time.zone.now.beginning_of_day)
end
(View)
<% #items_by_day.sort.reverse.each do |day, items| %>
<div class="row">
<h1><%= day.strftime("%d %B %Y") %></h1>
<div class="topAd">
<center>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- responsive -->
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-1876888588409540"
data-ad-slot="6100356041"
data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>
</div>
<% for a in items %>
<div class="videothumb col-xs-12 col-sm-4">
<div class="" style="
background-size: 100% 100%;
height: 240px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
overflow: hidden;">
<%= link_to a do %>
<div style="background: rgba(0,0,0, 0); width:100%; height: 85%;z-index:1000; position:absolute; top:0;"></div>
<% end %>
<iframe width="100%" height="100%" src="https://www.youtube.com/embed/<%= a.url %>" frameborder="0" allowfullscreen></iframe>
</div>
<div class="col-xs-12">
<h3>
<%= link_to a.title, a %> </h3>
</div>
<div class="col-xs-6">
<h2><%= a.artist %></h2>
</div>
<div class="col-xs-6">
<div style="text-align:right">
<%= link_to like_video_path(a), method: :put, class: "" do %>
<span class="glyphicon glyphicon-chevron-up">
<%= a.get_upvotes.size %>
<% end %>
<%= link_to dislike_video_path(a), method: :put, class: "" do %>
<span class="glyphicon glyphicon-chevron-down">
<%= a.get_downvotes.size %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
<% end %>
One of your where clauses is missing the assignment of its value for created_at.
I believe this line:
#items = Video.order("cached_votes_up DESC, created_at DESC").where("created_at ", Time.zone.now.beginning_of_day)
should be replaced by:
#items = Video.order("cached_votes_up DESC, created_at DESC").where("created_at >= ? ", Time.zone.now.beginning_of_day)