簡體   English   中英

NoMethodError:未定義的方法`permit&#39; #<Hash:0x007facebc78c98> 在 Rails Minitest 中

[英]NoMethodError: undefined method `permit' for #<Hash:0x007facebc78c98> in Rails Minitest

對於 rails Minitest 中的強參數,我收到此permit錯誤。 當我運行正常流程時(除了測試),這很好用 每當我運行我的測試時,它都會給我這個奇怪的錯誤。

為什么這僅在測試時不起作用

parameter

@params_return_type = {:client_notes_and_action_items=>[{"notes_action_type"=>"return_help", "title"=>"test title", "description"=>"", "to_do_action_items_attributes"=>{"0"=>{"linked_item_id"=>"987", "linked_item_type"=>"client_purchases_shipping_detail", "initial_request"=>"true"}}, "estimated_completion"=>'Mon, 30 Mar 2020', "assigned_to"=>"45", "assigned_by"=>"41"}],"client_id"=>"76576"}

運行測試時出錯

NoMethodError: undefined method `permit' for #<Hash:0x007facebc78c98>

你的意思是? 打印

this is my strong parameter

私人的

def  self.client_notes_and_action_item_params(params)
   params.permit( :client_id, :notes_action_type, :category, :description, :user_id, :image, 
   :comments, :status, :estimated_completion, :actual_completion, :title, 
   to_do_action_items_attributes: [:id, :linked_item_id, :linked_item_type, :deleted, 
   :initial_request])
end

我的 api 代碼就是這樣

params[:client_notes_and_action_items].each do |client_notes_and_action_item|
 ClientNotesAndActionItem.transaction do
 action_item = ClientNotesAndActionItem.new(client_notes_and_action_item_params(client_notes_and_action_item))

並且代碼在代碼的最后一行中斷

任何想法是什么問題。 如果強參數是一個問題,那么我應該為所有人收到此錯誤,正常流程僅在 Minitest 測試中斷時正常工作。

您需要將Hash對象轉換為ActionController::Parameters對象,因為permitActionController::Parameters一個方法

params = {:client_notes_and_action_items=>[{"notes_action_type"=>"return_help", "title"=>"test title", "description"=>"", "to_do_action_items_attributes"=>{"0"=>{"linked_item_id"=>"987", "linked_item_type"=>"client_purchases_shipping_detail", "initial_request"=>"true"}}, "estimated_completion"=>'Mon, 30 Mar 2020', "assigned_to"=>"45", "assigned_by"=>"41"}],"client_id"=>"76576"}
@params_return_type = ActionController::Parameters.new(params)

試一試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM