0

多年来我一直在做 RoR,但这是我使用 Mongo 的第一个项目(也是我的第一个 api-only 项目)。我在 HABTM 协会方面遇到了困难,我怀疑它与参数有关,但我不确定还有什么可以尝试的。

这是我所拥有的:

class Project
  include Mongoid::Document
  field :name, type: String
  field :start_date, type: Date
  field :target_date, type: Date

  has_and_belongs_to_many :users
end

class User
  include Mongoid::Document
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :jwt_authenticatable, jwt_revocation_strategy: JWTBlacklist

  field :email,              type: String
  field :_id, type: String, default: ->{ email }
  { ... devise stuff ...}
  has_and_belongs_to_many :projects
end

在我的项目控制器中,我有这个参数:

def project_params
      params.permit(:id, :name, :start_date, :target_date, :description, user_ids: [])
    end

是的,我也试过做 {user_ids: []}。

当我使用 Postman 发出 URL PUT 请求以尝试将用户添加到项目时,我收到“不允许的参数”错误。但是......我允许那个参数,对吧?

我有点发疯了,因为我不知道我是否遇到了 Mongo 问题、Rails 5 问题或 API 问题。所有其他电话都工作正常。

Started PUT "/rapi/projects/3" for 127.0.0.1 at 2017-02-15 22:55:10 -0500
Overwriting existing field _id in class JWTBlacklist.
Overwriting existing field _id in class User.
Processing by Api::V1::ProjectsController#update as JSON
  Parameters: {"user_ids"=>"test@gmail.com", "id"=>"3"}
MONGODB | localhost:27017 | anthem.find | STARTED | {"find"=>"users", "filter"=>{"_id"=>"test@gmail.com"}}
MONGODB | localhost:27017 | anthem.find | SUCCEEDED | 0.000363703s
Overwriting existing field _id in class Project.
MONGODB | localhost:27017 | anthem.find | STARTED | {"find"=>"projects", "filter"=>{"_id"=>"3"}}
MONGODB | localhost:27017 | anthem.find | SUCCEEDED | 0.000244022s
Unpermitted parameters: user_ids, format
Overwriting existing field _id in class Release.
Completed 204 No Content in 20ms

我会很感激任何关于我可能会尝试的想法。

4

2 回答 2

1

但是......我允许那个参数,对吧?

不完全的。你允许user_ids作为一个数组。但是您将其作为标量值发送。这足以让强参数不让数据通过。

下定决心,做一个(允许数组和发送数组)或另一个(允许标量和发送标量)。

于 2017-02-16T13:42:18.293 回答
0

我认为你应该使用 devise_parameter_sanitizer.permit

devise_parameter_sanitizer.permit(:id, :name, :start_date, :target_date, :description, keys: [:username])
于 2017-02-16T08:18:46.593 回答