I am trying to figure out a way to configure JSON with special fields, custom formats, etc. I created the as_json and to_xml method in my model to formulate the object as I need. This works well, but it is careless, because some of my helper methods had to move into the model, because I need formats in helpers and models. I also think that this is a sloppy code and takes the model out of control.
I managed to get the format of json.erb, but I do not think that it works 100% correctly, and the callback is not added either. Anyone get this job
Here is what I got so far.
api calls format.json
the template is called items.json.erb
<% @items.each do |item| %>
<%= { :item => { :id => item.id, :name => item.name }.to_json.html_safe %>
<% end %>
It works, but it seems weird. Anyone have suggestions or is there a way to do this?
btw did it for a callback to work
<%= params[:callback]+"(" if params[:callback] %>
<% @items.each do |item| %>
<%= { :item => { :id => item.id, :name => item.name }.to_json.html_safe %>
<% end %>
<%= ")" if params[:callback] %>
source
share