我在 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"}
Run Code Online (Sandbox Code Playgroud)
运行测试时出错
NoMethodError: undefined method `permit' for #<Hash:0x007facebc78c98>
Run Code Online (Sandbox Code Playgroud)
你的意思?打印
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
Run Code Online (Sandbox Code Playgroud)
我的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))
Run Code Online (Sandbox Code Playgroud)
代码在代码的最后一行中断
任何想法是什么问题。如果强参数是一个问题,那么我应该对所有参数都出现此错误,正常流程仅在 Minitest 测试中断时工作正常。