4152

Why Rails app showing database information at the bottom?

Question:

I have created a blog. And whenever I added a post, there is always a list of record from the database showing at the bottom of the post index page (home.html.erb), like this:

[#<Post id: 1, title: "hahaha", content: "Because the gravatar_for method is undefined, the u...", public: true, created_at: "2013-03-18 04:00:17", updated_at: "2013-03-18 04:01:09">]

I've tried to remove <%= will_paginate @posts %> but it doesn't work.

Here's my home.html.erb:

<%= @posts.each do |post| %> <article class="posts"> <h2><%= link_to post.title, post_path(post) %>

<h3><%= post.public %></h3>

<%= truncate markdown(post.content), length: 400, omission: " ......" %>

<span class="continue"><%= link_to "... Continue Reading ...", post_path(post) %></span> </article> <% end %> <%= will_paginate @posts %>

Here's my Gemfile just in case you need it:

source 'https://rubygems.org' gem 'rails', '3.2.12' gem 'pg' gem 'redcarpet' gem 'will_paginate' gem 'redcarpet' gem 'coderay' group :development, :test do gem 'rspec' gem 'rspec-rails' gem 'faker' end group :test do gem 'capybara' gem 'factory_girl_rails' end group :assets do gem 'sass-rails', '~> 3.2.3' gem 'coffee-rails', '~> 3.2.1' gem 'uglifier', '>= 1.0.3' end gem 'jquery-rails' gem 'bcrypt-ruby', '~> 3.0.0'

This is a weird situation. So I want to know what happened?

Thank you!

Answer1:

Change your template file from -

<%= @posts.each do |post| %> <article class="posts"> <h2><%= link_to post.title, post_path(post) %>

<h3><%= post.public %></h3>

<%= truncate markdown(post.content), length: 400, omission: " ......" %>

<span class="continue"><%= link_to "... Continue Reading ...", post_path(post) %></span> </article> <% end %> <%= will_paginate @posts %>

to -

<% @posts.each do |post| %> <article class="posts"> <h2><%= link_to post.title, post_path(post) %>

<h3><%= post.public %></h3>

<%= truncate markdown(post.content), length: 400, omission: " ......" %>

<span class="continue"><%= link_to "... Continue Reading ...", post_path(post) %> </span> </article> <% end %>

It shows up because you are using <%= @posts.each do |post| %> instead of <% @posts.each do |post| %>. <%= %> will output the return but <% %> won't

Answer2:

You need to change this:

<%= @posts.each do |post| %>

to this:

<% @posts.each do |post| %>

The <%= tells it to append the output to the HTML, which is why you're seeing the array.

Recommend

  • Does will pagination work with forms which have method=“POST”?
  • Rake aborting with error
  • Rails WillPaginate::Collection not paginating Array
  • Heroku installing sqlite3 even though it is not a dependecy
  • Is there a way to stop this default insert in rails
  • Dispose not working as expected in DirectorySearcher
  • Fluid video height
  • Weird LEFT OUTER JOIN on Includes eager loading of rails 3
  • Using ActiveRecord and Rails to insert Data into postgresql database get this error: RuntimeError: E
  • SQL append distinct values from two columns and two tables
  • HALF_PTR Windows data type
  • Count from each distinct date, fill in missing dates with zero
  • JQuery .html() remove line break on IE 8
  • Webgrid not refreshing after delete MVC
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • ActiveRecord query for a count of new users by day
  • Regex thinks I'm nesting, but I'm not
  • What is the “return” in scheme?
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Cannot Parse HTML Data Using Android / JSOUP
  • Comma separated Values
  • How to get icons for entities from eclipse?
  • How to disable jQuery.jplayer autoplay?
  • JTable with a ScrollPane misbehaving
  • Java static initializers and reflection
  • unknown Exception android
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • How to load view controller without button in storyboard?