Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've checked active_model_serializers, RABL and JBuilder. None of them allow to customize the JSON format that is received.</p> <p>For that one must deal with wrap_parameters, see <a href="http://edgeapi.rubyonrails.org/classes/ActionController/ParamsWrapper.html" rel="nofollow">http://edgeapi.rubyonrails.org/classes/ActionController/ParamsWrapper.html</a> It works, still the code is ugly: I get JSON stuff inside my controller + the serializer/model instead of one place.</p> <p>Example of use of wrap_parameters:</p> <pre><code>class EventsController &lt; ApplicationController wrap_parameters :event, include: [:title, :start, :end, :allDay, :description, :location, :color] def create respond_with Event.create(params[:event]) end end </code></pre> <p>and then inside my model (Frederick Cheung is right on this part):</p> <pre><code>class Event &lt; ActiveRecord::Base attr_accessible :title, :start, :end, :allDay, :description, :location, :color # JSON input allDay is all_day alias_attribute :allDay, :all_day # JSON input start is starts_at # +datetime+:: UNIX time def start=(datetime) self.starts_at = Time.at(datetime) end # JSON input end is starts_at # +datetime+:: UNIX time def end=(datetime) self.ends_at = Time.at(datetime) end # Override the JSON that is returned def as_json(options = nil) { id: id, title: title, start: starts_at, # ISO 8601, ex: "2011-10-28T01:22:00Z" end: ends_at, allDay: all_day, description: description, # Not rendered by FullCalendar location: location, color: color } end end </code></pre> <p>For info ASP.NET MVC (with Json.NET) does it using C# decorator attributes which is pretty elegant:</p> <pre><code>class Post { [JsonPropertyAttribute("title")] public string Title; } </code></pre> <p>I have created a gist that shows how to implement serialization/deserialization: <a href="https://gist.github.com/3858908" rel="nofollow">https://gist.github.com/3858908</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload