In Rails Erb how do I loop in desc order? - ruby-on-rails

Hi I am trying to create a news feed but I want the newest post to display at the top. So basically I want to display in desc order. I'm sure I could come up with a solution for this but I was wondering if any one knows of the best and easiest way like if there is a method already supplied by rails to do this?
<h3 class="title">News Feed</h3>
<div class="news-content">
<% Post.all.each do |post| %>
<div class="post">
<div class="title"><%= post.title %></div>
<div class="date"><%= post.created_at.strftime('%b %-d') %></div>
<div class="content"><%= raw post.description %></div>
</div>
<% end %>
</div>

you make use of scope.
Create scope latest_first in Post model
class Post < ActiveRecord::Base
scope :latest_first, order('created_at DESC')
end
and use the scope in view
<h3 class="title">News Feed</h3>
<div class="news-content">
<% Post.latest_first.find_each do |post| %>
<div class="post">
<div class="title"><%= post.title %></div>
<div class="date"><%= post.created_at.strftime('%b %-d') %></div>
<div class="content"><%= raw post.description %></div>
</div>
<% end %>
</div>
you can give suitable name to scope based on its use

I would suggest to split the data-loading logic away into the controller ...
class PostsController < ApplicationController
def index
#posts = Post.order(id: :desc)
end
end
The controller layer specifies what data to load and how to do it. I also prefer to explicitly state the order using the ActiveRecord DSL. Then in your view you can just do ....
<h3 class="title">News Feed</h3>
<div class="news-content">
<% #posts.each do |post| %>
<div class="post">
<div class="title"><%= post.title %></div>
<div class="date"><%= post.created_at.strftime('%b %-d') %></div>
<div class="content"><%= raw post.description %></div>
</div>
<% end %>
</div>

Related

Loop through posts that is promote

How to loop through post that are on promoted. I have a method in my post model that I can access. How would I call it in this instance. For example here are my methods my post model.
class Post < ApplicationRecord
def promoted?
subscriptions.present?
end
def self.promoted_posts
Post.joins(:subscriptions).where(:subscriptions => {:is_active => true})
end
def self.not_promoted_posts
Post.left_outer_joins(:subscriptions).where(:subscriptions => {:post_id => nil})
end
def self.ordered_posts
Post.promoted_posts + Post.not_promoted_posts
end
end
Here is my view. I would like to loop through all the posts that are promoted which is the method "def promoted?" in my model
<% #posts.take(6).each do |post| %>
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-4">
<div class="featured-box">
<figure>
<%= link_to image_tag(post.images.first, class: 'img-fluid'), post %>
</figure>
<div class="feature-content">
<div class="product">
<p><%= post.category.name %> / <%= post.subcategory.name %></p>
</div>
<h4><%= link_to post.title, post %></h4>
<ul class="address">
<li>
Ad#: <%= post.ad_number %>
</li>
<li>
posted <%= time_ago_in_words(post.created_at) %> ago
</li>
<li>
<%= post.state%> , <%= post.city.downcase %>
</li>
</ul>
<div class="listing-bottom">
<h3 class="price float-left">$<%= post.price%></h3>
<%= link_to 'Feature Ad', post, class: 'btn-verified float-right', style: 'color: red;' %>
</div>
</div>
</div>
</div>
<% end %>
If you are desperately in need of using the promoted, you can try
Post.all.find_all(&:promoted?)
which is short notation of
Post.all.find_all do |post|
post.promoted?
end
which is kinda n+1 query and opting it while you can just use
Post.joins :subscriptions
is a fail.

Rails: order items in nested view

I have a model called Post which belongs_to another model named Group. So currently a user can navigate to a group and create a post, the post is displayed under the correct view. However, I'm trying to figure out how to make the posts display in descending order so that the newest post is on top for that particular group.
this is in the posts_controller. It places them in descending order correctly when I go to .../groups/:id/posts however I need the posts to display descending in .../groups/:id
# GET /groups/:group_id/posts
def index
#posts = #group.posts.order("created_at desc")
end
This is in the groups/show.html.erb view. Is there a way to add a descending order onto #group.posts.each ?
<%= link_to 'New Post', new_group_post_path(#group.id) %>
<!-- gets posts for the group -->
<div class="row">
<% #group.posts.each do |post| %>
<div class="col-lg-12">
<div class="card bottom-pad">
<div class="card-body">
<h4 class="card-title"><%= link_to post.title, group_post_path(#group, post), class: "text-primary" %></h4>
<p class="card-text"><%= truncate(post.content, length: 500) %><br><br>
<%= post.user.last_name %>
</p>
</div>
<div class="card-footer">
<small class="text-muted"><%= time_ago_in_words(post.created_at) + " ago" %></small>
</div>
</div>
<br><br>
</div><!--./col-->
<% end %><!--./#group.posts.each-->
</div><!--./row-->
You can do the ordering in your show.html.erb like this:
<%= link_to 'New Post', new_group_post_path(#group.id) %>
<!-- gets posts for the group -->
<div class="row">
<% #group.posts.order(created_at: :desc).each do |post| %>
<div class="col-lg-12">
<div class="card bottom-pad">
<div class="card-body">
<h4 class="card-title"><%= link_to post.title, group_post_path(#group, post), class: "text-primary" %></h4>
<p class="card-text"><%= truncate(post.content, length: 500) %><br><br>
<%= post.user.last_name %>
</p>
</div>
<div class="card-footer">
<small class="text-muted"><%= time_ago_in_words(post.created_at) + " ago" %></small>
</div>
</div>
<br><br>
</div><!--./col-->
<% end %><!--./#group.posts.each-->
</div><!--./row-->

I'm trying to render two columns of information in my rails app

So, I have an app that is trying render 2 (or maybe more if needed) columns using a loop. It does one column and looks pretty good, but I want the option of 2 or even more. I know about "in_groups.of()", but I can't quite figure it out to work with my
<% #vendors.each do |vendor| %>
<%= link_to vendor do %>
<div class="row">
<div class="col-md-6">
<div class="card-container">
<div class="col-md-6">
<div class="card">
<%= image_tag attachment_url(vendor, :background_image), class: 'card-img-top' %>
<div class="card-block">
<h4 class="card-title"><%= vendor.Company %>. </h4>
<p class="card-text"><%= vendor.Description.html_safe.first(25) %></p>
<div class="card-standing"><strong><%= vendor.FinancialStanding %></strong></div>
</div>
</div>
</div>
</div>
</div>
</div>
<% end %>
<% end %>
Not sure why you needed this, because everything can be managed with css and html. But you can make changes like this:
<% #vendors.to_a.in_groups_of(2).each do |vendor| %> # #vendors.to_a cover AR to array
<% vendor.each do |v| %> # becuase vendor is array here.
..........your code..............
<% end %>
<% end %>

Rails 4 - ordering scopes

Im trying to make an app in Rails 4.
I have two scopes in my qualifications model:
scope :completed, ->{ where(pending: !true) }
scope :pending, -> { where(pending: true) }
I am trying to list them (newest first) in my view.
I have this view file:
<% Qualification.pending.sort_by.year_earned.asc.each do |qualification| %>
<div class="row">
<div class="col-md-12">
<div class="profilequalifications">
<%= qualification.current_study %>
</div>
</div>
</div>
<% end %>
<% Qualification.completed.sort_by(&:year_earned).each do |qualification| %>
<div class="row">
<div class="col-md-12">
<div class="profilequalifications">
<%= qualification.completed_award %>
</div>
</div>
</div>
<% end %>
The second index works - but in the wrong order.
The first index - I have tried a million variations on the expression but can't find one that doesnt throw an error.
I have tried each of the above, and the following (each of which are following examples I found on this site):
<% Qualification.pending.sort_by(&:year_earned).reverse_order.each do |qualification| %>
<% Qualification.pending.sort_by(&:year_earned.reverse).each do |qualification| %>
<% Qualification.pending.sort_by('&:year_earned ASC').each do |qualification| %>
<% Qualification.pending.sort_by('year_earned ASC').each do |qualification| %>
Rather than list them all out - does anyone know how to list in ascending order?
I would include the order inside your scope.
scope :completed, ->{ where(pending: !true).order('year_earned DESC') }
scope :pending, -> { where(pending: true).order('year_earned DESC')}
And in your view just remove .sort_by and it should work then.
<% Qualification.pending.each do |qualification| %>
<div class="row">
<div class="col-md-12">
<div class="profilequalifications">
<%= qualification.current_study %>
</div>
</div>
</div>
<% end %>
<% Qualification.completed.each do |qualification| %>
<div class="row">
<div class="col-md-12">
<div class="profilequalifications">
<%= qualification.completed_award %>
</div>
</div>
</div>
<% end %>
I think you want Qualification.pending.order(year_earned: :asc).each do |qualification|
See this for more information: http://apidock.com/rails/ActiveRecord/QueryMethods/order

Ruby on Rails View Rendering DB Info On Page

I am working on a project and currently working on one of the views which is a page of different categories. Everything is rendering correctly however it's also putting the db info in the page.
Here is the code of my view
<div class="categories">
<div class="container blurbs">
<div class="cards row">
<%= #categories.each do |c| %>
<div class="card col-xs-4" %>
<%= image_tag c.image, :class => "cat" %>
<h4 class="title"><%= c.title %></h4>
</div>
<% end %>
</div>
</div>
</div>
Here is a link to a
screenshot of rendered page
Yes, fix is:
<div class="categories">
<div class="container blurbs">
<div class="cards row">
<% #categories.each do |c| %>
<div class="card col-xs-4" %>
<%= image_tag c.image, :class => "cat" %>
<h4 class="title"><%= c.title %></h4>
</div>
<% end %>
</div>
</div>
</div>
Look I removed = from this <%=.. In the below line :
<% #categories.each do |c| %>
#each method returns the collection after it completed its iterations. And due to this <%=, the return value of each which is #categories printed back. But if you use <%.. only, all above things will happen, but it wouldn't print back the object #categories.
when you use the tags <%= ... %> whatever is within the tags gets displayed on the page. In your current view you have
<%= #categories.each do |c| %>
<div class="card col-xs-4" %>
<%= image_tag c.image, :class => "cat" %>
<h4 class="title"><%= c.title %></h4>
</div>
<% end %>
Which displays the entirety of whatever the loop returns which is where you're getting the display. Change the tags to be <% #categories.each do |c| %> and you'll be good to go.

Resources