如何在 rails 4 中使用 params.require [英] how to use params.require in rails 4

查看:40
本文介绍了如何在 rails 4 中使用 params.require的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的注册表格的私有方法,它有四个字段,firstnameemailpasswordconfirm密码.我不知道如何检查密码确认.

I have a private method like this for a registration form which has four fields, firstname, email, password and confirm password. I am not sure how to check for password confirmation.

def user_params
    params.require(:user).permit(:name, :email, :password, 
               :password_confirmation)
end

以前,我使用下面的代码.如何将下面的代码转换为使用 params.require

Previously, i was using the below code.How do i convert the code below to use params.require

User.new(name: params[:name], email: params[:email], 
     password: params[:password], confirmpassword: params[:password])

推荐答案

看你的代码

User.new(name: params[:name], email: params[:email], 
     password: params[:password], confirmpassword: params[:password])

我怀疑您没有使用 password_confirmation 字段.在这种情况下,这就是您将如何使用 params.require

I suspect that you're not using password_confirmation field. In that case, this is how you will use params.require

def user_params
    params.require(:name)
    params.require(:email)
    params.require(:password)
    params.permit(:name,:email,:password)
end

根据评论更新

如果您正在使用 password_confirmation 字段,那么这应该类似于 RAILS 3.x.x

if you are using password_confirmation field, then this should be like in RAILS 3.x.x

User.new(name: params[:name], email: params[:email], 
     password: params[:password], confirmpassword: params[:password_confirmation])

而且,这就是 strong_parameters(通常用于 rails 4.x.x)

And, this is how it will be with strong_parameters (usually with rails 4.x.x)

User.new(user_params)

def user_params
    params.require(:name)
    params.require(:email)
    params.require(:password)
    params.require(:confirm_password)
    params.permit(:name,:email,:password,:confirm_password)
end

这篇关于如何在 rails 4 中使用 params.require的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆