使用每个do时显示所有post的信息

all post's information displayed when using each do

我有 "advices" 属于一个用户。用户有很多建议。 我尝试在工作正常的视图中显示当前用户的每个建议名称。但是我不知道为什么所有其他建议的信息也显示在建议名称列表的末尾(建议 id、名称、内容、created_at、updated_at、user_id)。谢谢你的帮助。

查看:

<% if user_signed_in? %>
  <%= current_user.advices.each do |c| %>
    <ul>
      <li><%= c.name %> - <%= c.content %></li>
    </ul>
  <% end %>
  <% else %>
  <p> you have to log in</p>
<% end %>

控制器:

  def index
    @advices = Advice.all
  end

型号:

class Advice < ActiveRecord::Base
    belongs_to :user
end

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :advices
end

架构:

ActiveRecord::Schema.define(version: 20160329192355) do

  create_table "advices", force: :cascade do |t|
    t.string   "name"
    t.text     "content"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id"
    t.boolean  "reseau"
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.integer  "user_id"
    t.boolean  "reseau"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

看到这个了吗?

<%= current_user.advices.each do |c| %>

真的应该

<% current_user.advices.each do |c| %>

您一直在输出数组和您的建议。