2

I have defined param below in my spec file:

let(:params) do
 {
   company: {
    name: "My company",
    company_type_ids: [1,2]
   }
 }
end

There's no problem when I send request with the same param by postman, but when calling post request with rspec array of int in param is converted to string, like this: company_type_ids: ["1", "2"]

It should be company_type_ids: [1,2]

How can I prevent that array in param to be converted to string?

Many thanks!

4
  • 2
    You can't. The data passed to server via http(s) will always be textual or binary(file upload). Why you need ids as integer? Commented Apr 7, 2020 at 7:27
  • @AmitPatel Because I need to handle that integer array before create or update. I have a fixed list of types, and I want to get names of those type ids from list using method "select" and "include?" Commented Apr 7, 2020 at 8:38
  • 1
    You can still use string ids to search records e.g CompanyType.find(id: params[:company_type_ids]. No need to convert string to integer. Commented Apr 7, 2020 at 9:37
  • Actually you can pass integer array as long as you set the header CONTENT_TYPE to be application/json. Commented Aug 26, 2021 at 18:53

1 Answer 1

0

How to fix this problem in the post request:

post '/route', params: { 
  array: [1,2] 
}, as: :json
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.