Bower with Rails
Javascript dependancies and sprockets
- tags
- javascript
- rails
Here’s some simple code to accept a JSON string posted to a Sinatra end point rather than a form. I switched from using jQueries $.ajax
method to superagent as part of my exploration of the node javascript package universe, and it has a different way of serializing nest objects when posting as a form. Specifically, it doesn’t.
I needed something that could do both.
This first tries and loads the parameters using the normal form encoding methods. If it doesn’t find the path
parameter, it attempts to parse the body’s payload, found in request.body.read
, using JSON.parse
:
|
|
The coffeescript that calls this is:
|
|
In this case, @state.metadata
is also a complex object, and posting it as JSON ensures that it’s marshalled correctly.
Not too complicated.
Previously
Next