https://github.com/greena13/jason-form
Converts JSON objects to array of Rails-friendly form-data key-value tuples
https://github.com/greena13/jason-form
json rails
Last synced: about 1 year ago
JSON representation
Converts JSON objects to array of Rails-friendly form-data key-value tuples
- Host: GitHub
- URL: https://github.com/greena13/jason-form
- Owner: greena13
- License: isc
- Created: 2015-12-05T12:43:00.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2018-02-14T08:22:23.000Z (over 8 years ago)
- Last Synced: 2025-03-26T05:01:33.086Z (over 1 year ago)
- Topics: json, rails
- Language: JavaScript
- Size: 45.9 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jason-form
[]()
[](https://travis-ci.org/greena13/jason-form)
[](https://github.com/greena13/jason-form/blob/master/LICENSE)
Utility for converting arbitrarily deep JavaScript objects to FromData, using Rails conventions for nested arrays and objects.
If you use Rails views to generate and submit forms already, you do not need this library.
If you use some other method for generating your front end data, or have it as a JavaScript of JSON object, then this library can be considered the glue code needed to plug in to a Rails backend.
## Usage
```javascript
import jasonForm from 'jason-form';
const input = {
string: 'string',
array: [1,2],
object: {
foo: 'foo',
bar: 'bar'
}
};
const body = jasonForm.formData(input) // [['string', 'string'], ['array[]', 1], ['array[]', 2], ['object[foo]', 'foo'] ['object[bar]', 'bar']]
fetch('http://my.rails.server', {
method: 'POST',
credentials: 'include',
body,
headers: {
'Content-Type': 'multipart/form-data'
}
})
```
### Why not just submit the JavaScript object as a JSON request?
In most cases, this will work just fine and should indeed be the preferred approach. The issue is when you need to transmit data that is not supported by the JSON format (such as file upload data), or the backend that you need to send data to only accepts form data. Then this library becomes helpful.
### What if my JavaScript object isn't already in a rails-friendly format (_attributes suffixes, snake_cased keys, etc)?
Run it through [rails_request](https://github.com/greena13/rails-request) first.