Format date time in find operation in Rails 3

前端 未结 6 1784
情话喂你
情话喂你 2021-01-28 15:34

I\'m looking for a way to format date time in find(:all) so that when I render my results in JSON, the date time will look like

\"March 20, 2011\"

instead of

6条回答
  •  萌比男神i
    2021-01-28 15:47

    Look you use jbuilder? and for example index.json.jbuilder

    json.array!(@textstrings) do |textstring|
      json.extract! textstring, :id, :text
      json.created_at textstring.created_at.to_formatted_s(:short) 
      json.url textstring_url(textstring, format: :json)
    end
    

    in this example I am use method .to_formatted_s

     json.created_at textstring.created_at.to_formatted_s(:short
    

    and i've got

    [{"id":1,"text":"liveasda","created_at":"17 Nov 12:48","url":"http://localhost:5555/textstrings/1.json"},{"id":2,"text":"123","created_at":"17 Nov 14:26","url":"http://localhost:5555/textstrings/2.json"},
    

提交回复
热议问题