JSON Mapping
This recipe will help you to setup a basic JSON Mapping in your application.
You can use
JSON.mapping
to directly create an object from JSON:Then in your routes file:
Alternatively you can use
response_with
helper. Here you don't need to setup content_type
, however the requested path requires a .json
extension, by example /json_mapping.json
class SomeController < ApplicationController
def json_mapping
return "empty body" if request.body.to_s.blank?
user = User.from_json request.body.to_s
user.username += "mapped!"
response_with do
json user.to_json
end
end
end
Last modified 18d ago