Amm*_*mar 1 authentication passwords devise ruby-on-rails-3
我在我的Rails 3.0.9应用程序中使用Devise进行用户身份验证.由于我希望能够管理用户,因此我创建了以下用户控制器:
class UsersController < ApplicationController
def index
@users = User.all
end
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
flash[:notice] = "Successfully created User."
redirect_to users_path
else
render :action => 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
params[:user].delete(:password) if params[:user][:password].blank?
params[:user].delete(:password_confirmation) if params[:user][:password].blank? and params[:user][:password_confirmation].blank?
if @user.update_attributes(params[:user])
if current_user.update_with_password(params[:user])
sign_in(current_user, :bypass => true)
end
flash[:notice] = "Successfully updated User."
redirect_to users_path
else
render :action => 'edit'
end
end
def destroy
@user = User.find(params[:id])
if @user.destroy
flash[:notice] = "Successfully deleted User."
redirect_to users_path
end
end
end
Run Code Online (Sandbox Code Playgroud)
我这用于显示,创建和删除用户,但我在更新密码时遇到了问题.
当我更新当前登录帐户的密码时,它会自动将我退出.
在控制器中我尝试使用以下方法解决此问题(您可以在上面的代码中看到它)
if current_user.update_with_password(params[:user])
sign_in(current_user, :bypass => true)
end
Run Code Online (Sandbox Code Playgroud)
但这给了我这个错误 - >
undefined method `update_with_password' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
我真正想要的是能够更新任何帐户密码,而无需将其注销(因为管理员可以更改常规用户密码).
没有必要写
这段代码在控制器中
if current_user.update_with_password(params[:user])
sign_in(current_user, :bypass => true)
end
Run Code Online (Sandbox Code Playgroud)
相反,你应该继续下面的一个
if @user.update_attributes(params[:user])
sign_in(current_user, :bypass => true)
redirect_to users_path
end
Run Code Online (Sandbox Code Playgroud)
欢呼:)
| 归档时间: |
|
| 查看次数: |
4116 次 |
| 最近记录: |