Search code examples
ruby-on-railsruby-on-rails-4miniteststrong-parameters

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


i am getting this permit error for strong parameters in rails Minitest. This works fine when i run through normal flow(apart from testing) Whenever i run my test it gives me this weird error.

Why this doesn't work only while testing

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"}

Error while running test

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

Did you mean? print

this is my strong parameter

private

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

my api code is like that

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))

and code breaks here on the last line of the code

Any idea whats the issue. If strong parameter was an issue then i should get this error for all , normal flow works fine only Minitest test breaks.


Solution

  • You need to convert Hash object to ActionController::Parameters object as permit is a method of ActionController::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)
    

    Give it a try.