Reputation:
I have been struggling to figure out how to place my meta data for all pages. The head
tag where the meta data must be placed is on application.html.erb
. My approach at first which I later found as a bad practice was to check the URL path, like: if request.original_fullpath == '/faq'
to show the meta data for the FAQ page.
Despite being a bad practice, this couldn't help on pages that are dynamic (like the user's profile pages). Then I decided to take a simple approach and make it work. I added the meta tags on each page individually. I was naive thinking that can be overridden by using the head tag on each page. So I was wondering, how do I set the meta tags/data for each page on Rails? What's your approach?
Upvotes: 0
Views: 1191
Reputation: 10856
The content_for
helper might work well.
# app/views/layouts/application.html.erb
<head>
<%= content_for :meta_tags %>
</head>
# app/views/posts/show.html.erb
<% content_for :meta_tags do %>
...
<% end %>
Upvotes: 1