Ruby-on-rails 设计不验证密码/密码确认

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6543863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:30:22  来源:igfitidea点击:

Devise Not Validating Password/Password Confirmation

ruby-on-railsruby-on-rails-3devise

提问by Kyle Decot

I have a custom controller that handles the editing of user passwords based off of the code here.

我有一个自定义控制器,它根据此处的代码处理用户密码的编辑。

User Model

用户模型

attr_accessible :password, :password_confirmation, :username, :login
...
devise :database_authenticatable, 
       :lockable, 
       :registerable, 
       :recoverable, 
       :rememberable, 
       :trackable

PasswordsController

密码控制器

expose(:user) { current_user }

def update
  if user.update_with_password(params[:user])
    sign_in(user, :bypass => true)
    flash[:notice] = "success"
  else
    render :edit
  end
end

My edit password form is located here.

我的编辑密码表单位于此处

The problem is that no matter what I enter (or don't enter for that matter) into the edit password form, The "success" flash method is displayed.

问题是无论我在编辑密码表单中输入(或不输入)什么,都会显示“成功”闪光方法。

回答by sarahhodne

If you want Devise to do validations, you need to add the :validatablemodule to your model. This is fairly easy to do, just add :validatableto the list of module in the devisecall, so your model says:

如果您希望 Devise 进行验证,则需要将该:validatable模块添加到您的模型中。这很容易做到,只需:validatabledevise调用中添加到模块列表中,这样你的模型就会说:

devise
   :database_authenticatable, 
   :lockable, 
   :registerable, 
   :recoverable, 
   :rememberable, 
   :trackable,
   :validatable

This will make devise add validations.

这将使设计添加验证。

Another easy way is to add your own validations. If you just want to validate that the password confirmation matches, you can add a validates_confirmation_ofvalidation by adding this to your model:

另一种简单的方法是添加您自己的验证。如果您只想验证密码确认是否匹配,您可以validates_confirmation_of通过将其添加到您的模型中来添加验证:

validates_confirmation_of :password

I hope this helps.

我希望这有帮助。

回答by Jigar Bhatt

I think you forgot to initialize strong parameter in application_controller.rb in rails 4

我想你忘了在 rails 4 中的 application_controller.rb 中初始化强参数

before_action :configure_permitted_parameters, if: :devise_controller? protected

before_action :configure_permitted_pa​​rameters, if: :devise_controller? 受保护

 def configure_permitted_parameters    
    devise_parameter_sanitizer.for(:sign_up){|u|u.permit(:email,:password,:password_confirmation)}
 end  

回答by Muhammad Sannan Khalid

find your object for updation in controller.

在控制器中找到要更新的对象。

user = User.find_by_id(params[:id])
    unless user.blank?
      if user.update_attributes(params[:user])
        flash[:notice] = "User updated successfully."
        redirect_to "somwhere"
      else
        render :action => 'edit'
      end
    else
      render :action => 'edit'
    end

if you don't want to update the old password then add these line before updation so the new code will be:

如果您不想更新旧密码,请在更新前添加这些行,以便新代码为:

    user = User.find_by_id(params[:id])
        unless user.blank?
          params[:user].delete(:password) if params[:user][:password].blank?
          params[:user].delete(:password_confirmation) if params[:user][:password_confirmation].blank?
if user.update_attributes(params[:user])
            flash[:notice] = "User updated successfully."
            redirect_to "somwhere"
          else
            render :action => 'edit'
          end
        else
          render :action => 'edit'
        end

write somthing like this in user.rb model

在 user.rb 模型中写这样的东西

devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :locakable