foxeshavedens foxeshavedens -4 years ago 100
HTML Question

How to extract my own page title using Rails

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download