我有属于用户的“建议”。用户有很多建议。 我尝试在视图中显示当前用户的每个建议的名称,哪个工作正常。但是我不知道为什么所有其他建议的信息也会显示在建议名称列表的末尾(建议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
答案 0 :(得分:0)
看到这一点?
<%= current_user.advices.each do |c| %>
真的应该
<% current_user.advices.each do |c| %>
您一直在使用您的建议输出数组。