【发布时间】:2018-01-05 09:31:33
【问题描述】:
如何在页面中显示用户的名字,例如“你好,current_user.first_name”,我的架构
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "first_name", default: "", null: false
t.string "last_name", 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.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
当我尝试在我的页面 <span>Hello, <%= current_user.email %></span> 中进行操作时,一切正常,但是当我尝试 <span>Hello, <%= current_user.first_name %></span> 时,它什么也没显示。我的错在哪里?
【问题讨论】:
-
请分享您存储的当前用户的基本信息。
-
@asda111sd 如果你知道
current_user的id然后检查rails 控制台User.find(1).first_name.present?这里我给了1用户ID 你可以通过current_user id 检查它 -
current_user.id 工作正常
-
@asda111sd 你说的是同样的事情,但最终也不起作用。请更正。
-
@asda111sd 不,你没有明白我的意思,我的意思是在 Rails 控制台上检查 User.find(current_user_id).first_name.present?如果它返回真假?并将 current_user_id 替换为您的 current_user.id
标签: ruby-on-rails model-view-controller devise