How to display an array of object in ejs with conditions - mysql

i'm doing a web app to manage my money with node Js.
Here is what it's look like :
I've done almost everything, but I still have one part to do, which is to display my last 30 transactions.
I can't display my transactions as I want. It is necessary that if the date of the current transaction is different from the transaction just before then it displays the date and the transaction otherwise just the transaction. I block at the condition level if the date is different
At first I tried to display the first transaction with the date and then use (for) or (for in) to display the others with the condition but i can't use the i variable in my array of objects.
like this :
<%= transaction30[0].DateT%>
<%= transaction30[0].Name%>
<%= transaction30[0].Montant%>
<% for var i in transactions30 %> or <% for var i=1; i<= transactions30.length;i++%> {
<% if(transactions30[i].DateT != transactions30[i-1].DateT) %>
then display date and infos
<% }else{ %>
juste display infos
<% } %>
Then I read somewhere that you shouldn't use (for) for arrays of objects. So I tried a forEach.
Here is my code :
<h1>
<%= transactions30[0].DateT %>
</h1>
<% let status=transactions30[0].DateT %>
<% transactions30.forEach(function(item){ %>
<% console.log(item.DateT," et ",status) %>
<% if(item.DateT!=status) {%>
<h1>
<%= item.DateT %>
</h1>
<div class=" activite">
<div class="transaction-image"></div>
<div class="transaction-info-container">
<p class="transaction-nom">
<%= item.Nom %>
</p>
<p class="transaction-categorie">
<%= item.Categorie %>
</p>
</div>
<p class="transaction-montant">
<%= item.Montant %>
</p>
</div>
<% status=item.DateT %>
<% }else{ %>
<div class="activite">
<div class="transaction-image"></div>
<div class="transaction-info-container">
<p class="transaction-nom">
<%= item.Nom %>
</p>
<p class="transaction-categorie">
<%= item.Categorie %>
</p>
</div>
<p class="transaction-montant">
<%= item.Montant %>
</p>
</div>
<% } %>
<% }); %>
The 'prevdate' variable is updating well, but it always display date of all transactions. The (if) condition work but only for the first transaction because i dont get the date of the first transaction two times ( i display the first date at start, out of the forEach condition)
Here is the array for better comprehension : array named transactions30
Can someone help me please ?

If found the solution. Instead of (if date != previsousdate) i had to do
if(date < previousdate || date > previousdate)
and now it's work
<h1>
<%= transactions30[0].DateT %>
</h1>
<% let status=transactions30[0].DateT %>
<% transactions30.forEach(function(item){ %>
<% console.log(item.DateT," et ",status) %>
<% if(item.DateT>status || item.DateT < status) {%>
<h1>
<%= item.DateT %>
</h1>
<div class=" activite">
<div class="transaction-image"></div>
<div class="transaction-info-container">
<p class="transaction-nom">
<%= item.Nom %>
</p>
<p class="transaction-categorie">
<%= item.Categorie %>
</p>
</div>
<p class="transaction-montant">
<%= item.Montant %>
</p>
</div>
<% status=item.DateT %>
<% }else{ %>
<div class="activite">
<div class="transaction-image"></div>
<div class="transaction-info-container">
<p class="transaction-nom">
<%= item.Nom %>
</p>
<p class="transaction-categorie">
<%= item.Categorie %>
</p>
</div>
<p class="transaction-montant">
<%= item.Montant %>
</p>
</div>
<% } %>
<% }); %>

Related

Middleman Blog Pagination shows all articles

I have been trying to activate Pagination for my Middleman Blog, but for some reason, even though pagination itself seems to work (as signified by the buttons at the bottom, leading me until page three), all articles are shown on each individual page, instead of just ten
section in config.rb
activate :blog do |blog|
blog.name = "en"
blog.prefix = "blog"
blog.permalink = "/{year}/{month}/{day}/{title}.html"
blog.sources = "/posts/:year-:month-:day-:title.html"
blog.layout = "blog_layout"
blog.tag_template = "tag.html"
blog.paginate = true
blog.page_link = "page:num"
blog.per_page = 10
end
Blog Index.html
---
pageable: true
blog: en
priority: 1.0
change_frequency: weekly
description: Tbd.
title: Blog
---
<div class="page-content">
<div class="container">
<ul class="blog-post-list">
<% blog.articles.each do |article| %>
<li>
<a href="<%= article.url %>">
<div>
<img src="<%= article.data.image %>" class="blog-post-list__image" />
</div>
<div class="blog-post-list__post-preview">
<p class="blog-post-list__title"><%= article.title %></p>
<p><%= article.summary(140, '...') %></p>
<span><%= article.date.strftime('%B %e, %Y') %></span>
</div>
</a>
</li>
<% end %>
<ul class="paginate">
<% if paginate && num_pages > 1 %>
<% if prev_page %>
<p class="previous"><%= link_to 'Previous page', prev_page %></p>
<% end %>
<% end %>
<% if paginate %>
<% if next_page %>
<p class="next "><%= link_to 'Next page', next_page %></p>
<% end %>
<% end %>
</ul>
</ul>
</div>
</div>
Any hints what could be the issue here, that causes all articles to be displayed anyways would be highly appreciated! Thanks in advance :)
Is that not because you use blog.articles.each and should use page_articles instead?
I think you have to change blog.articles.each (the total number of articles) and use something like <% page_articles.each do |article| %> to get the number of articles you want on your page.

Ruby: undefined method `find_by_username'

I've been following a tutorial Follower Tutorial and I keep getting the error undefined method `find_by_username' for the line.
<%= render '/components/follow_button', :user => User.find_by_username(params[:id]) %>
show.html.erb
<%= render '/components/follow_button', :user => User.find_by_username(params[:id]) %>
<div class="panel panel-default">
<div class="panel-body">
<h5 style="color: grey; font-size: 125%;">Who to follow</h5>
<% for #u in #toFollow do %>
<p style="font-weight: bold; opacity: 0.85;" ><%= #u.username %></p>
<% end %>
</div>
</div>
user.rb
def unfollow(other)
active_relationships.find_by(followed_id: other.id).destroy
_follow_button.html.erb
<% if current_user.id != user.id %>
<div class="panel panel-default">
<div class="panel-body">
<center>
<% if !current_user.following?(user) %>
<%= form_for(current_user.active_relationships.build) do |f| %>
<div><%= hidden_field_tag :followed_id, user.id %></div>
<%= f.submit "Follow", class: "btn btn-primary" %>
<% end %>
<% else %>
<%= form_for(current_user.active_relationships.find_by(followed_id: user.id),
html: { method: :delete }) do |f| %>
<%= f.submit "Unfollow", class: "btn" %>
<% end %>
<% end %>
</center>
</div>
</div>
I've been messing around for ages now and can't work it out. Any help would be appreciated. Feel free to ask for more code etc.
In newer version of rails you should use following code:
Inside controller:
#user = User.find_by(username: username)
In Rails 4 onwards the find_by_... methods are deprecated therefore your User.find_by_username will not work for Rails 4 and higher. You should be able to use either:
User.find(id)
or
User.where(:username=> username)

How can I add "link_to 'Back to search result'" feature Rails

I have views/search/search_textbook.html.erb that shows the results of search. When a user click textbook.title by <%=link_to "#{textbook.title}", textbook_path(textbook.id) %>, The user is invoking the show method from textbooks_controller.
However, inside views/textbooks/show.html.erb has <%= link_to 'Back to list', textbooks_path %> that only take user to the index page of textbook (views/textbooks/index.html.erb).
below is my page that shows the search results views/search/search_textbook.html.erb:
<div class="container">
<div class="row">
<div class="col-sm-12">
<table class = "table table-striped">
<h1>Textbook Search Results</h1>
<thead>
<tr>
<th>Textbook Title</th>
<th>Subject</th>
<th>Price</th>
<th>Accept Offer</th>
<th>Created on</th>
</tr>
</thead>
<tbody>
<% #textbooks.each do |textbook| %>
<tr>
<!--<td><%= textbook.title %></td>-->
<td><%=link_to "#{textbook.title}", textbook_path(textbook.id) %></td>
<td><%= textbook.subject %></td>
<td>$<%= textbook.price %></td>
<td>
<% if textbook.offer == true %>
<!--<%= "\u2713"%>-->
<p class="offer_yes">✓</p>
<% else %>
<!--<%= "\u2715" %>-->
<p class="offer_no">✕</p>
<%end%>
</td><!--somehow below need Date.parse()-->
<td><%= textbook.created_at.strftime("%d %b. %Y") %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<div>
<%= form_tag search_textbook_path, :method => :get do %>
<p>
<a style="color:#000000" title="Suggest!" data-toggle="popover" data-trigger="hover" data-content="Title or Subject">
<%= text_field_tag :search, params[:search], placeholder: "Search textbooks" %>
</a>
<%= submit_tag "Search", :name => nil, class: "btn btn-success btn-sm" %>
<% end %>
</div>
<%= link_to 'Sell Textbook', new_textbook_path, class: "btn btn-primary" %>
<%= link_to 'Back to list', textbooks_path %>
<script>
$(document).ready(function(){
$('[data-toggle="popover"]').popover();
});
</script>
</div>
</div>
</div>
below is my views/textbooks/show.html.erb file:
<div class="container">
<div class="row">
<!--<div class="col-xs-offset-4 col-xs-4 col-xs-offset-4">-->
<div class="col-sm-offset-4 col-sm-4 col-sm-offset-4">
<!--<p id="notice"><%= notice %></p>-->
<p>
<h4><strong>Title:</strong>
<%= #textbook.title %></h4>
</p>
<p>
<strong>Subject:</strong>
<%= #textbook.subject %>
</p>
<p>
<strong>Price:</strong>
$<%= #textbook.price %>
</p>
<p>
<strong>Accept Offer:</strong>
<%if #textbook.offer == true%>
<%='Yes'%>
<%else%>
<%='No'%>
<%end%>
</p>
<p>
<strong>Description:</strong>
<pre><%= #textbook.description %></pre>
</p>
<p>
<% if !(#textbook.thumbnail_content_type =~ /^image/).nil? %>
<strong>Image:</strong>
<pre><%= image_tag #textbook.thumbnail.url(:medium) %></pre>
<%else%>
<!--<strong>No Image.</strong>-->
<%end%>
</p>
<p>
<strong>Created on:</strong>
<%= #textbook.created_at.strftime("%d %b. %Y") %>
</p>
<p>
<!--<%= link_to 'Contact', new_contact_path %>-->
<%= link_to 'Contact', new_contact_textbook_path(#textbook), class: 'btn btn-info' %>
</p>
<%if #textbook.user_email == current_user.email %>
<%= link_to 'Edit', edit_textbook_path(#textbook) %> |
<%= link_to 'Back to list', textbooks_path %>
<%else %>
<%= link_to 'Back to list', textbooks_path %>
<%end%>
</div>
</div>
</div>
How can I add a clickalbe button back to search results IF ONLY IF the user is came from search results??
Thanks!
This is how I would have done it.
1) Find the current path and assign it to a flash:
flashes are not only used for messages, they are actually the simplest way to send temporary data to an other action.
So in the search action of the textbook controller:
flash[:last_search_path] = [the current path]
note: [the current path] isn't ruby code, we'll see later how to find it
2) Use the link in your textbook show view:
In textbook show view all you have to do is:
<% if flash[:last_search_path] %>
<%= link_to "back to search results", flash[:last_search_path] %>
<% end %>
But... how do you find [the current path] ?
Obviously [the current path] isn't ruby code, so you'll have to replace it with something else.
there's multiple ways to find the current path or url: look here and here
Flashes ?
Flashes can be very useful for passing temporary data, I highly suggest you to read more about them. Learn more about flashes here
Other solutions to pass data between actions
You can use params to pass that link parameters but it would add more complexity. read more about params here
You can also use cookies but that would save it to the users browser and wouldn't work only if he came from the search result: read more about sessions here
Above flash is good way. But I use below way.
<p>
<%if URI(request.referer).path == "/search_textbook" %>
<%= link_to 'Back to Search Result', request.referer, class: "btn btn-warning" %>
<%end%>
</p>

Insert HTML with embedded Ruby

Is there a way that I can insert HTML tags with embedded Ruby?
I'm trying to loop over items in my database and need to put them in a bootstrap grid.
<% #div = "</div> <div class='row'> <div class='col-md-3'>" %>
<% #end = "</div>" %>
<div class="row">
<% #branches.each_with_index do |b, index| %>
<div class="col-md-3">
<div class="cardje transition">
<h2 class="transition"><%= b.description %></p>
<div class="cta-container transition">Import branch</div>
<div class="cardje_circle transition"></div>
</div>
</div>
<% if index+1%4 == 0 %>
<%= #div.html_safe %>
<% end %>
<% if index+1 == #branches.count %>
<% #end.html_safe %>
<% end %>
<% end %>
There must be a simpler way but I need this page to be done by tomorrow morning and I've been working way too long.
When I check the source code it's running only the loop and never implementing anything (so everything is on one row and overlapping).

Some HTML for values of Hash keys

I am building a panel with a partial, but I'd like to customise the inside of the panel with some html. Is there any way to write some HTML for a hash value?
I have a custom partial _panel_builder.html.erb that I takes as argument pclass, heading, body, etc., that I would like to use like this :
(The below syntax is bad, but I don't really understand how I could do something nice..)
<% #etudes.each_with_index do |etude, i| %>
<%= render 'shared/panel_builder',
pclass: panel_color_rotation(i),
heading: etude.name,
# For the body param, I'd like to be able to use some HTML with occasional <%=...%> for variables, like :
body: (%>
<p><%=etude.description %></p>
<ul>
<%etude.competences.each do |comp| %>
<li><strong><%= competence.name %></strong> : <%=competence.level %>
<br><small><%=competence.why %></small>
</li>
<% end %>
</ul>
<%).html_safe,
collapsable: true %>
<% end %>
EDIT : An idea of what my _panel_builder partial looks like :
<%
collapsable ||= false
pclass ||= "default"
footer ||= false
%>
<div class="panel panel-<%= pclass %> <%= if collapsable then "panel-collapsable " end %>">
<div class="panel-heading">
<%= heading %>
<% if collapsable %>
<span class="pull-right"><i class="glyphicon glyphicon-chevron-down"></i></span>
<% end %>
</div>
<div class="panel-body <%= if collapsable then "collapse" end %>">
<%= body %>
</div>
<% if footer %>
<div class="panel-footer <%= if collapsable then "collapse" end %>">
<%= footer %>
</div>
<% end %>
</div>
Okay so I was actually looking for the capture helper :
<% #etudes.each_with_index do |etude, i| %>
<%= render 'shared/panel_builder',
pclass: panel_color_rotation(i),
heading: etude.name,
body: capture do %>
<p><%=etude.description %></p>
<ul>
<%etude.competences.each do |comp| %>
<li><strong><%= competence.name %></strong> : <%=competence.level %>
<br><small><%=competence.why %></small>
</li>
<% end %>
</ul>
<% end %>,
collapsible: true %>
<% end %>
Note : in some cases, I also want to pass a complex HTML block as the header of footer, so I can't just have a helper that takes a block.
Note2 : My panel is a HTML template made for generic data. I cannot pass it a Ruby object