From the course: Hands-On Introduction: Ruby on Rails
Unlock the full course today
Join today to access over 24,400 courses taught by industry experts.
Using a helper to add a date - Ruby on Rails Tutorial
From the course: Hands-On Introduction: Ruby on Rails
Using a helper to add a date
- [Narrator] Let's add the date to each post. Open the post partial. And create a gap over the image tag. Add a P tag with some styles. A vertical margin of five. And text in gray. With intensity 600. Inside, create a time tag. With a post creation date. Use the raise function time ago in words as a text, which we'll use natural language to say the relative date. Get the post creation date and include the seconds as well. At the end, add ago. Let's see how it looks. Start server. Reload the list. And the date appears above its image. If you want to make this functionality reusable for other parts of the application, you must create a helper, which is a method that you can access from any template. This scaffold generated the posts helper file, where you can place post related functionality for templates. Create a new post created add method, which accepts a post as a parameter. Copy the function name with the parameter. Go back to the post partial and paste the call here. Cut the time…