# Step 1: Install the 'time_ago_in_words' helper method
# Add the following line to your Gemfile:
gem 'actionview'
# Then run:
bundle install
# Step 2: Use the 'time_ago_in_words' method in your Rails view
# In your view file (e.g., app/views/posts/show.html.erb), use the method like this:
<%= time_ago_in_words(@post.created_at) %> ago
# Step 3: Ensure the necessary module is included
# Make sure your controller includes the necessary module by adding the following:
include ActionView::Helpers::DateHelper
# Step 4: Define the '@post' variable in your controller
# In your controller (e.g., app/controllers/posts_controller.rb), define the '@post' variable:
def show
@post = Post.find(params[:id])
end
# Step 5: Ensure the 'created_at' attribute is present in your 'Post' model
# In your model file (e.g., app/models/post.rb), ensure the 'created_at' attribute is present:
class Post < ApplicationRecord
# ... other code ...
validates :created_at, presence: true
end