-3

I've seen multiple ways to extract metadata from other web pages, but is there a simple way to extract my own page title, description, etc?

I want to store the metadata in a variable so that I can call to_query on it and put it in a link.

Ideally, without using JavaScript.

RyanQuey
  • 705
  • 1
  • 9
  • 29
  • 1
    You want to parse the page you rendered and extract information from the DOM? That sounds super inefficient. Why not just design your layout to capture the `` content for you? – tadman Mar 01 '17 at 18:21
  • 2
    The answer is "Yes." If you want a more specific answer, ask a more specific question. – Wes Foster Mar 01 '17 at 18:22

1 Answers1

0

The title of a page is specified in its html; therefore it is impossible (ie impractical) to reference the title of a page directly from the page itself and use it to render more content to the page before serving it. You have two options:

1) Use javascript to modify the page after it has been completely rendered. (Not recommended)

2) Modify your rails application so that the title content is accessible anywhere in the view. (Recommended) This stackoverflow answer outlines an elegant way to create a title variable which can be used to set and access the title from anywhere in the view.

Note that in rails, the default title for each page is just the name of your rails app, and it's hardcoded into layouts/application.html.erb. So there really isn't much for you to "capture." If you want your pages to have unique titles, you need to program the back-end for that from scratch, as it were.

Community
  • 1
  • 1
eiko
  • 5,110
  • 6
  • 17
  • 35