Episode completed

Next epsiode starting in 3 seconds

Published at

Script

To top things up, let’s add a timestamp to our posts. We want to see when a post was created. IHP makes this very simple; Just open up the schema editor and add a field named “created at”. This will automatically set the type to Timestamp and the default value to the current time. So if you create a post, the time and date is automatically stored. Press “Update DB” to save the changes. To access and display the timestamp, let’s go to the show view again. Here we can wrap the “created at” value of the post in a p tag. While timestamps are really good at storing timestamps, they are not particularly great looking. To mitigate this, we can use timeAgo. This uses javascript to display the time in a relative way. So if we create a new post, you can see how many seconds passed since the post was created. We can also use the createdAt value to sort the blog posts in the index view by when they were created. To do that, we need to modify the PostsAction. This currently just queries all posts. We can add this simple order function to sort by date. By ordering the date in a descending order, the newest post is always on top.

Discussion