为什么Rails应用程序在底部显示数据库信息?

时间:2013-03-18 04:23:12

标签: ruby-on-rails ruby-on-rails-3

我创建了一个博客。每当我添加帖子时,总会有一个记录列表显示在帖子索引页面底部(home.html.erb),如下所示:

[#<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">] 

我尝试删除<%= will_paginate @posts %>,但它不起作用。

这是我的home.html.erb

<%= @posts.each do |post| %>
<article class="posts">
    <h2><%= link_to post.title, post_path(post) %></h1>
    <h3><%= post.public %></h3>
    <p><%= truncate markdown(post.content), length: 400, omission: " ......" %></p>
    <span class="continue"><%= link_to "... Continue Reading ...", post_path(post) %></span>
</article>
<% end %>
<%= will_paginate @posts %>

这是我的Gemfile以防你需要它:

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'

这是一种奇怪的情况。所以我想知道发生了什么?

谢谢!

2 个答案:

答案 0 :(得分:3)

你需要改变这个:

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

到此:

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

<%=告诉它将输出附加到HTML,这就是你看到数组的原因。

答案 1 :(得分:3)

从 -

更改模板文件
<%= @posts.each do |post| %>
<article class="posts">
  <h2><%= link_to post.title, post_path(post) %></h1>
  <h3><%= post.public %></h3>
  <p><%= truncate markdown(post.content), length: 400, omission: " ......" %></p>
  <span class="continue"><%= link_to "... Continue Reading ...", post_path(post) %></span>  
  </article>
  <% end %>
<%= will_paginate @posts %>

到 -

<% @posts.each do |post| %>
  <article class="posts">
  <h2><%= link_to post.title, post_path(post) %></h1>
  <h3><%= post.public %></h3>
  <p><%= truncate markdown(post.content), length: 400, omission: " ......" %></p>
  <span class="continue"><%= link_to "... Continue Reading ...", post_path(post) %>  </span>
 </article>
<% end %>

它出现是因为你正在使用&lt;%= @ posts.each do | post | %GT;而不是&lt;%@ posts.each do | post | %取代。 &lt;%=%&gt;将输出退货但&lt; %%&gt;不会