ruby on rails - Format Active Record Data before rendering as JSON -


i have data table going convert ajax , have format json values right formats.

in example how format tables.created_at time-stamp mm/dd format...

tables = table.where(:state => "missouri")  respond_to |format|   format.json { render json: tables } end 

in dom loop through data , needed it: chronic.parse(s.created_at.to_date).strftime("%m/%d")

and can see have alot of additional formatting data before returned json:

  <% if @load_search.nil? %>       <tr></tr>   <% else %>       <% @load_search.each |s| %>           <tr id="row_<%= s.id %>">             <td align="center">               <%= s.id %>             </td>             <td class="count" align="center">               <%= s.results %>             </td>             <td align="center">               <%= chronic.parse(s.created_at.to_date).strftime("%m/%d") %>             </td>             <td align="center">               <% if s.equipment.empty? %>                   equipment               <% else %>                   <%= s.equipment.pluck(:code).to_s.gsub("[","").gsub(']','').gsub('"','') %>               <% end %>             </td>             <td align="center">               <% if s.origin_id.nil? %>                   <%= s.origin_states %>               <% else %>                   <%= location.find(s.origin_id).cs unless s.origin_id.nil? %>               <% end %>             </td>             <td align="center">               <%= s.origin_radius_cs %>             </td>             <td align="center">               <% if s.dest_id.nil?  %>                   <%= s.dest_states %>               <% else %>                   <%= location.find(s.dest_id).cs unless s.dest_id.nil? %>               <% end %>             </td>             <td align="center">               <%= s.dest_radius_cs %>             </td>             <td align="center">               <%= s.length.to_s + ' ft.' unless s.length.nil? %>             </td>             <td align="center">               <%= s.weight.to_s + ',000 lbs' unless s.weight.nil? %>             </td>             <td align="center">               <% if s.ltl %>                   partial               <% elsif s.both %>                   full               <% else %>                   both               <% end %>             </td>             <td align="center">               <%= chronic.parse(s.pickup.to_date).strftime("%m/%d") %>             </td>             <td align="center">               <%= chronic.parse(s.delivery.to_date).strftime("%m/%d") unless s.delivery.nil?%>             </td>           </tr>       <% end %>   <% end %> 

you can override activerecord getter as

class model   def created_at      self[:created_at].strftime("%m/%d")   end end 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -