如何使用 GET 在 Rails api 中将参数作为散列传递?
How to pass parameter as hash in Rails api with GET?
我尝试在 url 中将参数作为散列传递给邮递员,就像这样
http://localhost:3000/api/v1/bill?album=3&song=4&song=7&album=6
我使用这段代码获取参数
def param_hash
params.permit(:album, :song)
end
并打印这个值param_hash.to_h
这是我想要的值{"album"=3, "song"=>4, "album"=>6, "song"=>7}
但实际上这就是我得到的 {"album"=>6, "song"=>7} ,最后只有 1 个哈希。
有没有办法把url中的所有hash值都取出来?
def param_hash
params.permit(album: [], song: [])
end
http://localhost:3000/api/v1/bill?album[]=3&song[]=4&song[]=7&album[]=6
根据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 => [])
http://localhost:3000/api/v1/bill?data={album: 3,song: 4,song: 7,album: 6 }
使用上述逻辑并允许 'data' 对象。
我尝试在 url 中将参数作为散列传递给邮递员,就像这样
http://localhost:3000/api/v1/bill?album=3&song=4&song=7&album=6
我使用这段代码获取参数
def param_hash
params.permit(:album, :song)
end
并打印这个值param_hash.to_h
这是我想要的值{"album"=3, "song"=>4, "album"=>6, "song"=>7} 但实际上这就是我得到的 {"album"=>6, "song"=>7} ,最后只有 1 个哈希。
有没有办法把url中的所有hash值都取出来?
def param_hash
params.permit(album: [], song: [])
end
http://localhost:3000/api/v1/bill?album[]=3&song[]=4&song[]=7&album[]=6
根据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 => [])
http://localhost:3000/api/v1/bill?data={album: 3,song: 4,song: 7,album: 6 }
使用上述逻辑并允许 'data' 对象。