我如何访问用户详细信息?

时间:2019-10-29 06:55:49

标签: ruby-on-rails devise

我想访问用户详细信息。 devise在用户注册时已经为用户创建了电子邮件和密码。例如,我现在想在我的视图中(在页面上)查看当前用户的地址。我的架构已添加到下面。我已经尝试过<%= current_user.email %>,但是没有用。

ActiveRecord::Schema.define(version: 2019_10_22_183225) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "positions", force: :cascade do |t|
    t.string "tripnumber"
    t.string "activity"
    t.string "date"
    t.string "time"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  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.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "barge_name"
    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

end

3 个答案:

答案 0 :(得分:0)

正确设置设置并让用户登录后,应该可以通过控制器及其视图中的User访问您的用户(current_user实例)。如果current_user为{{1 }}表示该用户尚未登录。

Devise在看守中间件之上运行,并依靠会话对用户进行身份验证,检查是否设置了cookie。另外,如果您刚刚添加了devise,请重新启动服务器,以便使用中间件(并确保通过nil来启动中间件)

答案 1 :(得分:0)

似乎装置设计不正确。您应该重新检查the guide line

答案 2 :(得分:0)

在我的应用程序控制器和路由下面,通常当我打开localhost:3000时,它会向我显示职位/新页面,现在我被重定向到登录页面,这很好,但是登录后没有任何作用..

class ApplicationController < ActionController::Base
  before_action :authenticate_user!
end

routes:
Rails.application.routes.draw do
  devise_for :users

  root to: 'positions#new'

resources :positions


end