Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 26th, 2012  |  syntax: None  |  size: 0.62 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Rails - json.erb template
  2. <% @items.each do |item| %>
  3. <%= { :item => { :id => item.id, :name => item.name }.to_json.html_safe  %>
  4. <% end %>
  5.        
  6. <%= params[:callback]+"(" if params[:callback]  %>
  7. <% @items.each do |item| %>
  8.     <%= { :item => { :id => item.id, :name => item.name }.to_json.html_safe  %>
  9. <% end %>
  10. <%= ")" if params[:callback]  %>
  11.        
  12. items = Item.all
  13. render :json => items.to_json(:only => [:id, :name]), :callback => params[:callback]
  14.        
  15. class Item
  16.  
  17.   def date
  18.     return "1 year and 8 months" #obviously use logic here
  19.   end
  20.  
  21.   def as_json(args={})
  22.     super(:methods=>[:date], :only=>[:id=>:name])
  23.   end
  24. end