Rails query string parameter format when providing an API with filters
I'm opening up a few REST API calls to others to use. Some of them include search filters.
Let's say I have a grocery endpoint When I currently make calls I might use:
/grocery_items/index.json?types[]=fruit&types[]=deli
Leaving me with params[:types] as a nice collection.
However to make things easier for the folks consumi开发者_StackOverflow中文版ng my API I want to be able to offer something like this:
/grocery_items/index.json?types=fruit,deli
Seems trivial to just split my params into a collection but I'm wondering if there are pitfalls since this seems to be against the grain of how rails expects collections to arrive as params.
I don't see anything wrong with doing a quick params[:types].split(',')
to make calling your API easier to use. It's pretty common to do tricks with the query string, and this is a really tame change.
精彩评论