adding labels/categories to looped micropost - ruby-on-rails

I am following Hartl's screencast and we are iterating through microposts and showing them in the view:
<div class="span8">
<% if #user.microposts.any? %>
<h3>Microposts (<%= #user.microposts.count %>)</h3>
<ol class="microposts">
<%= render #microposts %>
</ol>
<%= will_paginate #microposts %>
<% end %>
</div>
</div>
However I want to label each micropost (Micropost 1, Micropost 2, etc...). How can I do that?

<%= render #microposts %> renders the partial app/views/microposts/_micropost.html.erb. So it should just be a case of editing that file.
In the tutorial, I believe the micropost.html.erb file looks like:
<tr>
<td class="micropost">
<span class="content"><%= micropost.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
</td>
</tr>
To add a label, you could do something like:
<tr>
<td class="micropost">
<span class="label"><%= "Micropost #{micropost.id}" %><span>
<span class="content"><%= micropost.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
</td>
</tr>
This will add the text 'Micropost 1', 'Micropost 2' ect above each of the microposts content
Edit
If you don't want to use ID and just want sequential numbering then you can change the code to iterate over each micropost and render them individually, passing in the count:
<% #microposts.all.each_with_index do |micropost, index| %>
<%= render micropost, locals: {index: index} %>
<% end %>
Then in your partial you can use the local variable index:
<tr>
<td class="micropost">
<span class="label"><%= "Micropost #{index}" %><span>
<span class="content"><%= micropost.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
</td>
</tr>

Related

the Ransack gem does not find the result

I am using the Ransack gem, I have a patient model, I am following the documentation, and I cannot get it to work.
I have also seen many blogs about it but nothing is working for me. Hope someone can help me
I show the code of the view and the controller
my controller:
def index
#q = Patient.ransack(params[:q])
#Patients = #q.result(distinct: true)
#pagy, #patients = pagy(current_user.patients.order(apellido: :asc), items:20)
end
my view:
<%= search_form_for #q do |f| %>
<%= f.text_field :apellido_cont, class: 'form-control' %>
<%= f.submit class: 'btn btn-primary' %>
<% end %>
<div class="row">
<div class="col-xl-3">
<h3>Mis pacientes</h3>
</div>
<div class="col-xl-6">
<div class="form-group has-search">
<span class="fa fa-search form-control-feedback"></span>
<input type="text" class="form-control" placeholder="Buscar">
</div>
</div>
<div class="col-xl-3">
<%= link_to new_patient_path, class:'nuevo' do %>
<i class="fas fa-plus"></i> NUEVO PACIENTE
<% end %>
</div>
</div>
<div class="alertas">
<% if flash[:notice] %>
<div class="alert alert-success" role="alert"><%= flash[:notice] %></div>
<% end %>
</div>
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Nombre</th>
<th scope="col">Contacto</th>
<th scope="col">Edad</th>
</tr>
</thead>
<% if #patients.each do |patient| %>
<tbody>
<tr>
<td class="datos"><%= patient.apellido %> <%= patient.nombre %></td>
<td><i class="fas fa-mobile-alt"></i> <span><% if patient.telmovil.blank? %>No hay dato<% else %><%= patient.telmovil %><% end %></span></td>
<td><%= patient.age %></td>
<td><%= link_to patient_path(patient), class:'ver' do %>
<i class="far fa-eye"></i>
<% end %></td>
</tr>
<% end.empty? %>
<h5 class="titulo">No tienes ningun paciente.</h5>
<% end %>
</tbody>
</table>
<div class="paginacion mt-3">
<%== pagy_bootstrap_nav(#pagy) if #pagy.pages > 1 %>
</div>
</div>
so I'm happy I came across this post. I have been using kaminari for years and had no idea about pagy and now I'm excited to try it!
My experience is with kaminari, so I had to google usage with pagy. It looks like to me you need to pass the entire query to the pagy method.
ie:
#pagy, #patients = pagy(Patient.ransack(params[:q]).result(distinct: true))
Hopefully that works!

Kaminari not limiting collection in Spree

For some reason Kaminari is not limiting child objects on the parent show view.
Can see the pagination links but the collection does not get limited.
What am I doing wrong?
View -
<% if #handbag.microposts.any? %>
<h3>Posts (<%= #handbag.microposts.count %>)</h3>
<div class="row">
<div class="col-md-8">
<ol class="microposts">
<% #handbag.microposts.each do |micropost| %>
<li id="micropost-<%= micropost.id %>">
<span class="user"><%= micropost.user.email %></span>
<span class="content"><%= micropost.content %></span>
<%= image_tag micropost.picture.url if micropost.picture? %>
<span class="timestamp">
Added <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
</li>
<% end %>
</ol>
<%= paginate #microposts %>
Controller -
def show
#handbag = Spree::Handbag.find(params[:id])
#microposts = #handbag.microposts.page(params[:page] || 1).per(10)
end
Thanks for any help.
You're looping through #handbag.microposts, which is the entire collection, instead of #microposts, which is the paginated collection.
So just change #handbag.microposts.each to #microposts.each

Displaying microposts' comments for user

Building upon Micropost's comments on users page (Ruby on Rails) , I was able to add new comments for each micropost. However, I'm having problem displaying back the comments for each post on the same page. The codes below didn't throw any error, so I'm thinking the each micropost's comments are not assigned correctly so it can't display.
Users > Microposts > Comments
users_controller.rb
def show
#user = Account.find(params[:id])
#microposts = #user.microposts
#micropost = Micropost.new
#comments = #micropost.comments
#comment = Comment.new
end
microposts/_micropost.html.erb
<li>
<span class="content"><%= micropost.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<span class="comments">
<% if micropost.comments.any? %>
<ul class="comments">
<%= render #comments%>
</ul>
<% end %>
<%= render "shared/form_comment", micropost: micropost %>
</span>
</li>
comments/_comment.html.erb
<li>
<span class="content"><%= comment.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(comment.created_at) %> ago.
</span>
</li>
Change <%= render #comments%> to <%= render micropost.comments %> to loop through the specific post's comments instead of using generic comment loop.
<li>
<span class="content"><%= micropost.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<span class="comments">
<ul class="comments">
<%= render micropost.comments %> # you can leave out the #any?
</ul>
</span>
</li>

Using the pluralize helper but having the number in one div and the text in another?

I have the following HTML markup:
<div class="votes meta engagement">
<span class="vote-count">
<span class="vote-total">
<strong><%= #post.total_votes %></strong>
</span>
</span>
<span class="vote-text secondary">votes by</span>
<span class='voted-user'>
<% #post.votes.each do |vote| %>
<%= link_to vote.user.username, vote.user %>
<% end %>
</span>
</div>
I can't do <%= pluralize(#post_total_votes, "vote") %> because I need that text to be right before by in the vote-text div.
How can I solve this situation?
Shouldn't this work for what you're trying to do?
<span class="vote-text secondary"><%= pluralize(#post.total_votes, "vote") %> by</span>
edit:
Then wouldn't this work? You can obviously combine the two rails calls together, but just wanted to make sure this is what you were going for.
<div class="votes meta engagement">
<span class="vote-count">
<span class="vote-total">
<strong><%= #post.total_votes %> <%= pluralize(#post.total_votes, "vote") %></strong>
</span>
</span>
<span class="vote-text secondary">by</span>
<span class='voted-user'>
<% #post.votes.each do |vote| %>
<%= link_to vote.user.username, vote.user %>
<% end %>
</span>
</div>

Rails - WillPaginate Conflict

I'm trying to paginate the messages a group has.
class GroupsController < ApplicationController
...
def show
...
#message = #group.messages.paginate(:page => params[:page])
end
<% unless #group.messages.empty? %>
<table class="messages" summary="Messages from Group">
<tr>
<td class="messages">
<span class="content"><%= messages.content%></span>
<span class="timestamp">
Posted <%= time_ago_in_words(messages.created_at) %> ago.
</span>
</td>
</tr>
</table>
<%= will_paginate #messagess%>
<%end %>
But when I try to open my group#show page, gives me the error: undefined method 'content' for #<WillPaginate::Collection:0x31a67f0>, but the line the error gives is the line where I create a new Message, that is in the group#show page as well:
<%= f.text_area :content, :rows=>5 , :cols=>49 %>
Any ideas?
You need to loop over the messages and print each one out individually.
Controller:
def show
#messages = #group.messages.paginate(:page => params[:page])
end
View:
<% unless #messages.empty? %>
<table class="messages" summary="Messages from Group">
<% #messages.each do |message| %>
<tr>
<td class="messages">
<span class="content"><%= message.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(message.created_at) %> ago.
</span>
</td>
</tr>
<% end %>
</table>
<%= will_paginate #messagess%>
<% end %>
To prevent the error on your form you will need to pass it a single instance of a message. e.g.
form_for(Message.new) do

Resources