我正在使用devise和devise_ldap进行我的rails身份验证。我正在尝试使用内置帮助器,当前用户在我的应用程序的欢迎页面上显示用户电子邮件。 这是我尝试使用的代码:
<% if user_signed_in? %>
<div>Signed in as... <%= current_user.email %></div>
<% end %>
当我登录应用程序时,出现错误;
undefined method `email' for nil:NilClass
这是我的routes.rb
Rails.application.routes.draw do
devise_for :users
resources :users
resources :systems do
member do
get :targets, :sources
end
root 'systems#index'
end
和我的用户控制器:
class UsersController < ApplicationController
authorize_resource
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
def index
@users = User.all.order("display_name asc")
end
# GET /users/1
def show
end
# GET /users/new
def new
@user = User.new
end
# GET /stories/1/edit
def edit
respond_to do |format|
format.html
format.js
end
end
# POST /stories
def create
@user = User.new(user_params)
respond_to do |format|
puts 'user controller'
if @user.save!
format.html { redirect_to user_path(@user), notice: 'User was successfully created.' }
else
format.html { render :new }
end
end
end
# PATCH/PUT /stories/1
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to user_path(@user), notice: 'User was successfully updated.' }
else
format.html { render :edit }
end
end
end
# DELETE /stories/1
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_path notice: 'User was successfully destroyed.' }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:display_name, :email, :username)
end
end
我的用户型号:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
before_create :rememberable_value
before_save :get_ldap_values
devise :ldap_authenticatable, :rememberable, :trackable, :validatable
def get_ldap_values
if self.username
self.email = Devise::LDAP::Adapter.get_ldap_param(self.username,"mail").first if Devise::LDAP::Adapter.get_ldap_param(self.username,"mail")
self.display_name = Devise::LDAP::Adapter.get_ldap_param(self.username,"displayName").first if Devise::LDAP::Adapter.get_ldap_param(self.username,"displayName")
end
end
# def role?(role)
# return !!self.roles.find_by_name(role.to_s.camelize)
# end
def email_required?
false
end
def email_changed?
false
end
def rememberable_value
self.remember_token ||= Devise.friendly_token
end
def name_to_display
if self.display_name
self.display_name
else
self.username
end
end
def password_required?
false
end
def password_match?
self.errors[:password] << "can't be blank" if password.blank?
self.errors[:password_confirmation] << "can't be blank" if password_confirmation.blank?
self.errors[:password_confirmation] << "does not match password" if password != password_confirmation
password == password_confirmation && !password.blank?
end
end
我不确定在成功登录后能够访问当前用户信息的原因是什么。
更新
以下是新路线档案:
Rails.application.routes.draw do
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
get "/users/sign_in" => "devise/sessions#new"
# delete "/logout" => "devise/sessions#destroy"
end
devise_for :users
authenticate(:user) do
resources :users
resources :reports
resources :change_logs, only: [:index, :show]
resources :systems do
member do
get :targets, :sources
end
resources :change_logs, module: :systems
resources :components do
resources :change_logs, module: :components
end
resources :c_relations
end
resources :integrations
get '/static_pages/home' # => 'static_pages#home', as: 'home'
root 'systems#index'
end
答案 0 :(得分:1)
在routes.rb中,您应该包含
之后的行devise_for :users
在一个块中
authenticate(:user) do
resources :users
[...]
end
答案 1 :(得分:1)
问题是我覆盖了设计映射和current_user。我删除了它们,并且能够在我的视图中访问当前用户。
答案 2 :(得分:0)
你有inject(function (_ServiceBInstance_: ServiceB) {
console.log(_ServiceBInstance_);
});
in your controller链吗?
如果您没有要求设计验证操作,则在登录后,Current_user可能为零。