Rails从无处添加API字段

时间:2019-07-11 10:31:50

标签: ruby-on-rails ruby devise

我正在使用RESTful创建一个Rails API Mode API。 我正在使用devise处理用户创建,并使用simple_token_authentication处理令牌生成和授权。

这是我发出提取请求的方式:

  async requestAccountCreation(data) {
    await fetch('http://localhost:3000/users', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: data
    })
      .then(response => response.json())
      .then(data => {
        console.log(data)
      })
      .catch(e => {
        console.log(e)
      })
  }

当我console.log() requestAccountCreation(data)中的数据时,这给了我一个有效的JSON字符串,因此这没有问题。这是发送到Rails服务器的内容:

{"firstname":"Tristan","lastname":"Vermeesch","username":"PlayBossWar","email":"titivermeesch2@gmail.com","address":"Rue Haute, 37 5550 Chairière","password":"jsoaod"}

现在是Rails部分,首先是我的user.rb

class User < ApplicationRecord
  acts_as_token_authenticatable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  validates :firstname, presence: true
  validates :lastname, presence: true
  validates :username, presence: true
  validates :address, presence: true
  validates :idcard, presence: true
end

多余的字段是我添加到devise为我生成的现有字段中的字段,我迁移了所有文件。

这是我使用的route

devise_for :users, :controllers => { registrations: 'registrations' }

如您所见,我添加了一个自定义控制器来处理额外的字段,这里是:

class RegistrationsController < Devise::RegistrationsController
  private

  def sign_up_params
    params.permit(:firstname, :lastname, :email, :password, :username, :idcard, :address)
  end

  def account_update_params
    params.permit(:firstname, :lastname, :email, :password, :current_password, :username, :idcard, :address)
  end
end

我是从StackOverflow帖子中得到的。

现在控制台中出现错误,它是:

Started POST "/users" for 127.0.0.1 at 2019-07-11 12:24:42 +0200
Processing by RegistrationsController#create as */*
  Parameters: {"firstname"=>"Tristan", "lastname"=>"Vermeesch", "username"=>"PlayBossWar", "email"=>"titivermeesch2@gmail.com", "address"=>"Rue Haute, 37 5550 Chairière", "password"=>"[FILTERED]", "registration"=>{"firstname"=>"Tristan", "lastname"=>"Vermeesch", "username"=>"PlayBossWar", "email"=>"titivermeesch2@gmail.com", "address"=>"Rue Haute, 37 5550 Chairière", "password"=>"[FILTERED]"}}
Unpermitted parameter: :registration
   (0.1ms)  begin transaction
  ↳ /home/tristan/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
  User Exists (0.3ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ?  [["email", "titivermeesch2@gmail.com"], ["LIMIT", 1]]
  ↳ /home/tristan/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
   (0.1ms)  rollback transaction
  ↳ /home/tristan/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
Completed 200 OK in 452ms (Views: 0.2ms | ActiveRecord: 5.2ms)

因此它表明存在一个不允许的参数:registration。 问题是,我不知道它来自哪里,我没有在前端传递它,因此必须进行设计,但是我不知道为什么会这样做。

按照建议禁用包装器后,这是我现在的错误:

Started POST "/users" for 127.0.0.1 at 2019-07-11 13:46:28 +0200
Processing by RegistrationsController#create as */*
  Parameters: {"firstname"=>"Tristan", "lastname"=>"Vermeesch", "username"=>"PlayBossWar", "email"=>"titivermeesch25@gmail.com", "address"=>"Rue Haute, 37 5550 Chairière", "password"=>"[FILTERED]"}
   (0.2ms)  begin transaction
  ↳ /home/tristan/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
  User Exists (0.5ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ?  [["email", "titivermeesch25@gmail.com"], ["LIMIT", 1]]
  ↳ /home/tristan/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
   (0.2ms)  rollback transaction
  ↳ /home/tristan/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
Completed 200 OK in 430ms (Views: 0.3ms | ActiveRecord: 10.9ms)

2 个答案:

答案 0 :(得分:3)

您已在初始化程序中打开config.wrap_parameters [检查文件 config / initializers / wrap_parameters.rb ]或[您]正在控制器中调用wrap_parameters()...参数默认情况下,将根据您控制器的名称将其克隆并包装在密钥中。

在这里看看: http://guides.rubyonrails.org/action_controller_overview.html#parameters http://api.rubyonrails.org/classes/ActionController/ParamsWrapper.html

在特定控制器或应用程序控制器中调用 wrap_parameters false 也会分别在控制器特定级别和整个应用程序中停止此行为。

答案 1 :(得分:1)

读取config/initializers/wrap_parameters.rb的内容。

然后,要么关闭换行(如文件注释中所述),要么允许键级别的参数,例如:

params
  .require(:registration)
  .permit(*%i[firstname lastname email password username idcard address])

Extra info