No commit activity in last 3 years
No release in over 3 years
Rack middleware that fixes the discrepancy between jQuery.param and how Rack parses nested queries.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
 Project Readme

rack-jquery-params

A Rack middleware that bridges the discrepancy between jQuery.param and how Rack parses nested params within query strings and post bodies.

Gem Version

If you use jQuery's get, post, put, or ajax methods then jQuery.param is run on your data. The problem is that jQuery.param turns this javascript object:

{test: [{this_is_an_object_inside_an_array:'yes'}]}

into a query string that Rack cannot read correctly:

test[0][this_is_an_object_inside_an_array]=yes

Rack expects all arrays to be sent as empty brackets (i.e., "test[]"). It parses jQuery's "test[0]" as a hash, resulting in the following params:

{
    :test => {
        :0 => {
            :this_is_an_object_inside_an_array => 'yes'
        }
    }
}

This gem fixes the above params and converts it to:

{
    :test => [
        {
            :this_is_an_object_inside_an_array => 'yes'
        }
    ]
}

Setting Up JSONR

Install the gem:

gem install rack-jquery-params

In your Gemfile:

gem 'rack-jquery-params', :require => 'rack/jquery-params'

Activate by including it your config.ru file:

use Rack::JQueryParams
run Sinatra::Application

Options

By default the JQueryParams fix is applied to all http methods, however you can change this functionality by setting the applies_to option with your chosen http method:

use Rack::JQueryParams :applies_to => :get

You can also set applies_to to be an array of http methods:

use Rack::JQueryParams :applies_to => [:get, :put, :delete]

License

Please refer to LICENSE.md.