2015-04-01 40 views
0

我有一个数据表,我要将其转换为Ajax,并且必须将JSON值格式化为正确的格式。将呈现为JSON之前的活动记录数据格式化为

在这个例子中如何将我格式化tables.created_at时间戳以MM/DD 格式...

tables = Table.where(:state => "Missouri") 

respond_to do |format| 
    format.json { render json: tables } 
end 

在DOM我会做,通过数据的环路,做这个地方,我需要它:Chronic.parse(s.created_at.to_date).strftime("%m/%d")

而且你可以看到我有很多其他格式的做数据则返回为JSON之前:

<% if @load_search.nil? %> 
     <tr></tr> 
    <% else %> 
     <% @load_search.each do |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? %> 
        All 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 %> 

回答

0

您可以覆盖ActiveRecord获取程序为

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