加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用authlogic更改密码 – 验证不捕获空白输入

发布时间:2020-12-17 02:26:21 所属栏目:百科 来源:网络整理
导读:我正在尝试创建一个表单,以允许用户更改其密码: 视图: - form_tag change_password_users_path do = error_messages_for :user,:header_message = "Please Try Again",:message = "We had some problems updating your account" %br = label_tag :password
我正在尝试创建一个表单,以允许用户更改其密码:

视图:

- form_tag change_password_users_path do

  = error_messages_for :user,:header_message => "Please Try Again",:message => "We had some problems updating your account" 
  %br

  = label_tag :password,"New password:"
  = password_field_tag "password"
  %br

  = label_tag :password_confirmation,"NConfirm new password:"
  = password_field_tag "password_confirmation"
  %br

  = submit_tag "Update Account"

控制器:

def change_password
  @user = current_user
  if request.post?
    @user.password = params[:password]
    @user.password_confirmation = params[:password_confirmation]
    if @user.save
      redirect_to user_path(current_user)
    else
      render :action => "change_password"
    end        
  end
end

当密码“太短”或密码与确认不匹配时,Authlogic会捕获验证错误,但在提交表单并且两个字段都空白时,Authlogic不会执行任何操作.
@ user.save必须返回true,因为我被重定向到’user_path(current_user)’.

密码实际上并未在数据库中更改.

谢谢你的帮助.

解决方法

我建议你打@ user.changed?像以下示例一样检查空白密码:

def change_password
  @user = current_user
  if request.post?
    @user.password = params[:user][:password]
    @user.password_confirmation = params[:user][:password_confirmation]
    if @user.changed? && @user.save
      redirect_to user_path(current_user)
    else
      render :action => "change_password"
    end
  end
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读