【问题标题】:how to use params.require in rails 4如何在 rails 4 中使用 params.require
【发布时间】:2014-08-06 05:46:08
【问题描述】:

我有一个像这样的私有方法用于注册表,它有四个字段firstnameemailpasswordconfirm password。我不知道如何检查password confirmation

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

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

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

【问题讨论】:

  • 你的字段名是confirmpassword吗?那么如果你想whitelist它,你可以这样做params.require(:user).permit(:name, :email, :password, :confirmpassword)

标签: ruby-on-rails ruby ruby-on-rails-4


【解决方案1】:

查看你的代码

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

我怀疑您没有使用password_confirmation 字段。在这种情况下,这就是你将如何使用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

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

而且,这就是 strong_parameters 的情况(通常是 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

【讨论】:

  • 我有密码确认栏?
  • 在这种情况下,您可以像password一样将password_confirmation字段列入白名单
  • 完美, 所以在我的情况下是 :confirm_password 对吗?因为我的名字字段是确认密码
  • @theJava 你应该说清楚你的字段名是confirm_password。目前你说的是confirm password
猜你喜欢
  • 2017-08-20
  • 2013-08-27
  • 2017-04-01
  • 2016-04-20
  • 2013-06-26
  • 2013-08-20
  • 2015-03-30
  • 1970-01-01
相关资源
最近更新 更多