我正在后端使用Rails 5 --api模式构建一个Angular 4/5应用程序。我还没有使用任何身份验证。
这是我的用户 user.ts
export class User {
name: string;
email: string;
password: string;
password_confirmation: string;
admin: boolean;
activated: boolean;
constructor(name: string, email: string, password: string, confirmPassword: string, admin: boolean, activated: boolean){
this.name = name;
this.email = email;
this.password = password;
this.password_confirmation = confirmPassword;
this.admin = admin;
this.activated = activated;
}
}
我正在向/users
发送帖子请求给Rails控制器Users#create
。
我的请求有效负载如下所示:
activated: true
admin: true
email: "me@example.com"
name: "Mike Glaz"
password: "password1"
password_confirmation: "password1"
当我pry
进入rails控制器Users#create
并输入params
时,我看到以下内容:
<ActionController::Parameters {"name"=>"Mike Glaz",
"email"=>"me@example.com", "password"=>"password1",
"password_confirmation"=>"password1", "admin"=>true,
"activated"=>true, "controller"=>"users", "action"=>"create",
"user"=>{"name"=>"Mike Glaz", "email"=>"me@example.com",
"admin"=>true, "activated"=>true}} permitted: false>
iinside my method user_params
当我params.require(:user)
时,它会带回
{"name"=>"Mike Glaz", "email"=>"me@example.com", "admin"=>true, "activated"=>true}
我无法弄清楚为什么params
有两组不同的参数以及为什么user
不包含password
和password_confirmation
。
内部routes.rb
我有
resources :users