Home > Blockchain >  How to pass parameter as hash in Rails api with GET?
How to pass parameter as hash in Rails api with GET?

Time:03-03

I try to pass params as a hash in url with postman like this

http://localhost:3000/api/v1/bill?album=3&song=4&song=7&album=6 

I use this code to get param

 def param_hash
    params.permit(:album, :song)
  end

and print this value param_hash.to_h

This is a value i want {"album"=3, "song"=>4, "album"=>6, "song"=>7} But in reality that's what i got {"album"=>6, "song"=>7} , just have only 1 hash in last.

Is there anyway to take all hash value in url?

CodePudding user response:

def param_hash
  params.permit(album: [], song: [])
end
http://localhost:3000/api/v1/bill?album[]=3&song[]=4&song[]=7&album[]=6 

According to https://github.com/rails/strong_parameters

The permitted scalar types are String, Symbol, NilClass, Numeric, TrueClass, FalseClass, Date, Time, DateTime, StringIO, IO, ActionDispatch::Http::UploadedFile and Rack::Test::UploadedFile.

To declare that the value in params must be an array of permitted scalar values map the key to an empty array:

params.permit(:id => [])

CodePudding user response:

http://localhost:3000/api/v1/bill?data={album: 3,song: 4,song: 7,album: 6 }

Use the above logic and permit the 'data' object.

  • Related