Rails 5 api,角4,用户参数不通过

时间:2017-12-16 14:43:55

标签: ruby-on-rails angular parameters

我正在后端使用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不包含passwordpassword_confirmation

内部routes.rb我有

resources :users

0 个答案:

没有答案