2017-02-16 30 views
0

我一直在做RoR多年,但這是我第一個使用Mongo(也是我的第一個api-only項目)的項目。我在HABTM協會工作很艱難,我懷疑它與params有關,但我不確定還有什麼可以嘗試的。Mongoid 6,Rails 5,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"=>"[email protected]", "id"=>"3"} 
MONGODB | localhost:27017 | anthem.find | STARTED | {"find"=>"users", "filter"=>{"_id"=>"[email protected]"}} 
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 

我很感激任何想法,我還有什麼可以嘗試。

回答

1

可是...我允許該參數,對不對?

不完全。您正在允許user_ids作爲數組。但是,您將其作爲標量值發送。對於強大的參數來說,這不足以讓數據通過。

決定你的想法,做一個(允許數組和發送數組)或其他(允許標量和發送標量)。

+0

啊!我的問題不是鐵軌問題,而是我真的不知道如何使用郵差。感謝您的迴應,解決了它! :) – nilatti

0

我認爲你應該使用devise_parameter_sanitizer.permit

devise_parameter_sanitizer.permit(:id, :name, :start_date, :target_date, :description, keys: [:username])