Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use params.require in rails 4

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

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])
like image 659
theJava Avatar asked Mar 20 '23 07:03

theJava


1 Answers

Looking at your code

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

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

UPDATED AS PER COMMENT

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])

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
like image 70
Paritosh Piplewar Avatar answered Mar 27 '23 10:03

Paritosh Piplewar