0

I'm new to Ruby on Rails and I just did a Blog tutorial to learn the basics. What I need to know now how to display in a post the date/time of creation and show also how old is it. I tried something but I cannot understand how to display the current local time of creation. Like I create the post in the US but in DK I see the local time. However, I would like to extend this to show also the post in a chronological way.

The code:

    <h2><%= @post.title %></h2>
    <p><%= @post.body %></p>
    <p> <%= @post.created_at.strftime('%B %d %Y, %l:%M%P, %z') %></p>
    <hr>
<%= link_to 'Edit', edit_post_path(@post), :class => 'btn btn-default'%>
<%= link_to 'Delete', post_path(@post),
            method: :delete,
            data: {confirm: 'Are you sure?'},
            :class => 'btn btn-danger'
%>
<hr>

<%= render 'comments/comments' %>
<%= render 'comments/form' %>
Jakub
  • 2,367
  • 6
  • 31
  • 82

1 Answers1

1

You can get how old it is with this method:

http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-time_ago_in_words

<%= time_ago_in_words(@post.created_at) %>

To show in a chronological way, you have to sort your database search based on the created_at attribute. Add this line in your Controller so you can have access in your view.

@post = Post.order('created_at DESC')

At your View you can iterate over the data this way:

<ol>
  <% @post.each do |p| %>
    <li><%= p.create_at %></li>
  <% end %>
</ol>

UPDATE 1

I found this answer in SO about getting the user local time, it explain how to achieve that in two ways:

How to display the time in user's timezone

UPDATE 2

Another simple way to solve your problem. Change the default timezone in your Rails so you can have the right time in your data saved in database from where you are (you, the host).

How to change default timezone for Active Record in Rails?

This way won't get the timezone from your clients, only the timezone you set as default to Rails (to get the timezone from the client go to #UPDATE 1).

Community
  • 1
  • 1
Pedro Gabriel Lima
  • 1,122
  • 1
  • 9
  • 24
  • Thanks for the answer. But you answered me only a part. Could you please answer also the rest if you can? That will help me to understand it better. By the way that was helpful. – Jakub Sep 21 '17 at 12:56
  • What exactly is the rest? Get the time from where the user is from? – Pedro Gabriel Lima Sep 21 '17 at 12:59
  • Not really, using the `@post.created_at.strftime('%B %d %Y, %l:%M%P, %z')` I see a date/time in this format `September 21 2017, 1:08pm, +0000` But the time is 1:08pm when where I'm now is 15:08. I would like to understand how to have this formatted to local time where I'm now. Hope you could understand my meaning. @PedroGabrielLima – Jakub Sep 21 '17 at 13:11
  • @Jakub, Are you in localhost or using a cloud server? I say that because Rails use time from your server. – Pedro Gabriel Lima Sep 21 '17 at 13:16
  • thanks, I saw the other answer but, How to use it what I saw there in my example? I'm sorry but the first time I'm using Ruby. I tried to use it but I had no fortune to see any output. – Jakub Sep 21 '17 at 13:17
  • I'm on my local machine working on the rails server. – Jakub Sep 21 '17 at 13:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/155000/discussion-between-jakub-and-pedro-gabriel-lima). – Jakub Sep 21 '17 at 13:34