1 Replies - 1704 Views - Last Post: 14 November 2016 - 07:15 AM Rate Topic: -----

#1 tonyal  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 85
  • Joined: 23-September 14

Rails - do while true.

Posted 16 October 2016 - 04:12 PM

Hello. I'm working on a mock-up blog, and in the admin dashboard I currently have the 5 latest user comments shown. As it is, it will display them if they have been approved or not, and I would like to only show them if they haven't been approved (I will be adding a button next to them to approve them quickly, without having to travel to the "approve section". I'm new to Rails, and was following a tutorial to get this far and I am now adding my own things.

This is the loop for the dash section to display the comments:
<% @comments.each do |comment| %>
	<div class="comment-head-dash clearfix">
		<div class="commenter-name-dash pull-left"><%= comment.visitor.fullname %></div>
		<div class="days-dash pull-right"><%= time_ago comment.created_at %></div>
	</div>
	<p class="comment-dash">
		<%= truncate(comment.message, length: 125, separator: '') %> 
				
	</p>
	<hr>
<% end %>



This is from the controller.
class Admin::DashboardController < Admin::ApplicationController
  def index
  	@posts = Post.last 5
  	@comments = Comment.last 3
  	@visitors = Visitor.last 5
  end
end



I've tried to place the "comment, status: false" in a few places to get it to show, but it won't work and I'm not sure if I would put that in the controller, or in the do loop. If anyone can show me where I'm going wrong I would very much appreciate it. Thank you!

This post has been edited by tonyal: 16 October 2016 - 04:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Rails - do while true.

#2 Denis1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 104
  • Joined: 29-July 09

Re: Rails - do while true.

Posted 14 November 2016 - 07:15 AM

Hi tonyal,

You will need a method on the comment modal to identify the approved ones. There are two way you can do about doing this.

1) Get all the comments and skip unless it is approved
<% @comments.each do |comment| %>
	<% next if comment.approved? # this will skip to the next comment if approved is true %>
	<div class="comment-head-dash clearfix">
		<div class="commenter-name-dash pull-left"><%= comment.visitor.fullname %></div>
		<div class="days-dash pull-right"><%= time_ago comment.created_at %></div>
	</div>
	<p class="comment-dash">
		<%= truncate(comment.message, length: 125, separator: '') %> 
				
	</p>
	<hr>
<% end %>



Downside in doing that is that you are loading all the comments which can be slow as you get more comments and when paginating where maybe miss counting.

2) A better way is to have an approved scope on the comment model, which means you only load the approved ones eg:

# In model/comment.rb
  scope :pending_approval, -> { where(approved: nil) }



# In the controller
class Admin::DashboardController < Admin::ApplicationController
  def index
  	@posts = Post.last 5
  	@comments = Comment.pending_approval.last 3
  	@visitors = Visitor.last 5
  end
end



# In view
<% @comments.each do |comment| %>
	<div class="comment-head-dash clearfix">
		<div class="commenter-name-dash pull-left"><%= comment.visitor.fullname %></div>
		<div class="days-dash pull-right"><%= time_ago comment.created_at %></div>
	</div>
	<p class="comment-dash">
		<%= truncate(comment.message, length: 125, separator: '') %> 
				
	</p>
	<hr>
<% end %>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1