Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove the text from the table at the end of displaying table data

I’m not really sure how to quote this question, but I’m making a clone of Reddit in Ruby on Rails.

On the index page I am displaying all of the posts I have in the table "Posts". But when I display them all, at the end of the last post a text shows up which is wrapped in square brackets and contains all of the information from the table. I just want to know how to remove this text.

Picture of problem

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is the code used to display the post:

 <%= @posts.each do |post| %>
  <div class="card p-3">
    <small class="mb-2"><strong><%= link_to "s\\" + post.sub.name, sub_path(post.sub) %></strong> Posted by <%= link_to "u\\" + post.user.username, profile_path(post.user.username) %> <%= time_ago_in_words post.created_at %> ago</small>
    <h4><%= link_to post.title, sub_post_path(post.sub, post) %></h4>
    <p><%= truncate post.body, length: 200 %></p>
  </div>
<% end %>

My posts controller:

class PostsController < ApplicationController
  before_action :authenticate_user!, except: [ :index, :show ]
  before_action :set_post, only: [:show]
  before_action :auth_subscriber, only: [:new]

  def index
    @posts = Post.all
  end

  def show
  end

  def new
    @sub = Sub.find(params[:sub_id])
    @post = Post.new
  end

  def create
    @post = Post.new post_values
    @post.user_id = current_user.id
    @post.sub_id = params[:sub_id]

    if @post.save
      redirect_to subs_path(@post.sub_id)
    else
      @sub = Sub.find(params[:sub_id])
      render :new
    end
  end

  private

  def set_post
    @post = Post.find(params[:id])
  end

  def auth_subscriber
    unless Subscription.where(sub_id: params[:sub_id], user_id: current_user.id).any?
      redirect_to root_path, flash: { danger: "You are not authorized to view this page" }
    end
  end

  def post_values
    params.require(:post).permit(:title, :body)
  end
end

>Solution :

<% @posts.each do |post| %>
  <div class="card p-3">
    <small class="mb-2"><strong><%= link_to "s\\" + post.sub.name, sub_path(post.sub) %></strong> Posted by <%= link_to "u\\" + post.user.username, profile_path(post.user.username) %> <%= time_ago_in_words post.created_at %> ago</small>
    <h4><%= link_to post.title, sub_post_path(post.sub, post) %></h4>
    <p><%= truncate post.body, length: 200 %></p>
  </div>
<% end %>

In ERB <%= is used to output the result of an expression to the buffer. #each in Ruby returns self so what you’re seeing on the page is the result of implicitly calling #to_s on an array.

#each is always used for its side effects and not its return value. In the case of an ERB template each iteration writes to the buffer.

This can be somewhat hard to grasp but remeber that any plaintext in a template is written directly to the buffer – using each just does it n number of times.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading