1

I have 4 html layout like below. . this is say is fix.html.erb

!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example Test</title>
</head>
<body>
<h1>Hello</h1>
</body>
</html>

Now I want to know If is there any way that I can change the title with the help of controllers? We can create an application layout and put the common codes there, but then that title will get applied to all the pages.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Suraj
  • 2,423
  • 12
  • 39
  • 75
  • possible duplicate of [Rails 3 - Ideal way to set title of pages](http://stackoverflow.com/questions/3059704/rails-3-ideal-way-to-set-title-of-pages) – Surya Oct 01 '14 at 09:41
  • possible duplicate of [How do I change the title of a page in rails?](http://stackoverflow.com/questions/185965/how-do-i-change-the-title-of-a-page-in-rails) – Brad Werth Oct 01 '14 at 18:49

1 Answers1

2

Try:

# Layout (needs to be *.html.erb, not plain .html)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title><%= yield :title %></title>
  </head>
  <body>
    <%= yield %>
  </body>
</html>

# View

<% provide(:title, 'My title') %>
<h1>Hello</h1>
BroiSatse
  • 44,031
  • 8
  • 61
  • 86
  • You should probably mention that the file should be a layout and named with erb i.e. **fix.html.erb** and not **fix.html**. – Surya Oct 01 '14 at 09:41