I can't set the title of a page no matter what I have tried to put in my .html.erb file

60 views
Skip to first unread message

Robert Phillips

unread,
Jan 13, 2018, 7:18:23 PM1/13/18
to Ruby on Rails: Talk
I can't set the title of a page no matter what I have tried to put in my  .html.erb file


>rails new testblah

>bundle install   <-- not necessary here but anyway.

>rails generate controller abc def

> rails s

I go  to http://127.0.0.1:3000 and I see the default template view   .\app\views\abc\def.html.erb

But even when I put  <title>asdf</title> it doesn't change the title.
Even if I do
<html><head><title>asdf</title></head><body></body></html>

it doesn't change the title.

If I go to the rendered HTML of the page, I can see what it's doing..

Suppose my .\app\views\abc\def.html.erb
is

<html>
  <head><title>aaaaa</title></head>
   <body></body>
  </html>

What gets rendered is this


<!DOCTYPE html>
<html>
<head>
<title>Test15</title>
<meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="bDkNOUtlUKmlo1uzMRTqzCUCzBLX0q4/0NM8OfSXccoHocfYeZNFe+TsR8a4wvZhKakKRlXbU+NjWsnp4hNfXg==" />

<link rel="stylesheet" media="all" href="/assets/abc.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" data-turbolinks-track="reload" />
<link rel="stylesheet" media="all" href="/assets/application.self-f0d704deea029cf000697e2c0181ec173a1b474645466ed843eb5ee7bb215794.css?body=1" data-turbolinks-track="reload" />
<script src="/assets/rails-ujs.self-817d9a8cb641f7125060cb18fefada3f35339170767c4e003105f92d4c204e39.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/turbolinks.self-2db6ec539b9190f75e1d477b305df53d12904d5cafdd47c7ffd91ba25cbec128.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/abc.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/action_cable.self-69fddfcddf4fdef9828648f9330d6ce108b93b82b0b8d3affffc59a114853451.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/cable.self-8484513823f404ed0c0f039f75243bfdede7af7919dda65f2e66391252443ce9.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/application.self-eba3cb53a585a0960ade5a8cb94253892706bb20e3f12097a13463b1f12a4528.js?body=1" data-turbolinks-track="reload"></script>
</head>

<body>
<html>
<head><title>aaaaa</title></head>
<body></body>
</html>
</body>
</html>

So, what it is doing is rails is taking its own template then it dumps my template into its <body></body>

So the title isn't getting set


Hassan Schroeder

unread,
Jan 13, 2018, 7:46:39 PM1/13/18
to rubyonrails-talk
On Sat, Jan 13, 2018 at 4:18 PM, Robert Phillips
<robert.p...@gmail.com> wrote:

> So, what it is doing is rails is taking its own template then it dumps my template into its <body></body>

Yep, that's the way it works, though "template" is the wrong word.

This: http://guides.rubyonrails.org/layouts_and_rendering.html
may help.

If you want something within the scope of a layout like a title to be
dynamic you can assign the value in your controller and pass it in
as a variable, e.g. <title>@title</title>

HTH!
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Ralph Shnelvar

unread,
Jan 13, 2018, 7:59:42 PM1/13/18
to Robert Phillips
Robert,

To amplify Hassan's answer a bit ... and you probably already know this ...

You can only have one head section and, I believe, it has to be the first tag after the html tag.  If I remember correctly, most browsers will ignore misplaced head tags.

Rails will not look at the rendered text and say, "Oh my, Robert put in a head section.  Let me move it immediately after the HTML tag."

Ralph Shnelvar
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
rubyonrails-ta...@googlegroups.com.
To post to this group, send email to
rubyonra...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/aaa37d93-5198-4554-a440-0b9decbfee3f%40googlegroups.com.
For more options, visit
https://groups.google.com/d/optout.




Ralph

Robert Phillips

unread,
Jan 13, 2018, 11:01:27 PM1/13/18
to Ruby on Rails: Talk


On Sunday, 14 January 2018 00:46:39 UTC, Hassan Schroeder wrote:
On Sat, Jan 13, 2018 at 4:18 PM, Robert Phillips
<robert.p...@gmail.com> wrote:

> So, what it is doing is rails is taking its own template then it dumps my template into its <body></body>

Yep, that's the way it works, though "template" is the wrong word.

This: http://guides.rubyonrails.org/layouts_and_rendering.html
may help.

If you want something within the scope of a layout like a title to be
dynamic you can assign the value in your controller and pass it in
as a variable, e.g.  <title>@title</title>



Thanks, I can see what was happening now..

Whatever view is displayed, if displays the html from here

.\app\views\layouts\application.html.erb

which specifies a title and some other tags.

And that file says

<body>
  <%= yield %>
</body>


 And then so when trying to access '/',   it went to some specified controller some action, e.g. blah#bleh,  then it rendered the application.html.erb file, and inserted within it, the blah\bleh.html.erb file.      A fix was to rename  application.html.erb

Why is template not an appropriate name..  Isn't any ERB file a template, since you can insert data into it?

Also I notice that when I do  root 'application#a'  and I have in my application controller   def a end,  then I http to '/' then it runs the action but it can't find the template.. Is there anywhere that I can put a.html.erb that the rails server would find it?  Or does the application controller not have a corresponding template for each action?

Mike

unread,
Jan 14, 2018, 6:14:05 AM1/14/18
to Ruby on Rails: Talk
app/views/layouts/application.html.erb is rendered around your code, and do in the example above your page title is Title15 if I remember correctly.

If you want a dynamic page title you can put a line in the file such as

<%= render(:header_title) %>

in the head section of application.html., then you can use the following line in your underlying erb files

<% content_for(:header_title) do %>
<title>This is the page title</title>
<% end %>

What ever is in the block will be rendered in the header section (it could be html or JavaScript, or anything else that is valid where the render line is placed

Walter Lee Davis

unread,
Jan 14, 2018, 9:41:17 AM1/14/18
to rubyonra...@googlegroups.com
I think you may be getting hung up on the word template. Rails defines two different terms for what I suspect you are thinking of here: template and layout. A template is specific to a particular action in a particular controller, for example /views/widgets/show.html.erb would be a template automatically invoked for widgets_controller.rb's show method. A template only contains the code necessary to render the "guts" of the page, it never includes head or html elements or any of the repeated bits you may use to center the page or set up a grid. Think of a template as containing only the answer to the question: "What makes this page different than any other page in the site?"

A layout, on the other hand, is the outermost parts of the page only, with one line in the middle that reads <%= yield %>. That line is replaced by everything else that has been rendered so far. (Very often you will see that there is only one of these layouts for an entire site.) This has a number of benefits for you as a developer. For one thing, you don't have to repeat yourself in each template by creating all the outer page code. For another, Rails can precompile that code and have everything ready to go when the rest of the template sandwich is prepared for a new request.

All of the HTML generated by Rails is created in inside-out order. For example, partials are rendered for individual lines of a table, then the table is rendered in a template, then the template is inserted into the body (the layout) and the page is complete.

When you want to make parts of the layout dynamic, as you do, then the right place to do that is with a helper method. Helpers can be called within the template (where the variable data is known) and then cause an effect in the final rendering through the content_for mechanism. Here's one that I use:

app/helpers/application_helper.rb

def title(content)
content_for :title, "#{content} | Site Name Here"
end

In the application.rb.html layout, I have a title tag, like this:

<title><%= content_for?(:title) ? yield(:title) : "Site Name Here" %></title>

This gives me a generic title if I haven't bothered to set it, but allows me to pass in the title I want to show simply by setting the content_for :title key as you saw in the helper.

Finally, in the show template for my widget page, I would add this line anywhere in the code (order is not important, because it will always be rendered before the layout is):

<%- title @widget.product_name %>

And that sets the title on the layout for me.

Now you could set this `content_for` value in the controller, too, but I do it in the view because I usually add another layer to the helpers like this:

def headline(content)
title(content)
content_tag :h1, content
end

So in my template, where I want the H1 tag with the page headline on it, I simply do this:

<%= headline @widget.product_name %>

And now I have a perfectly matched H1 and title, for optimal SEO performance. I put the headline in the page where I want it to appear, and there's no additional effort to make the title tag.

Walter

> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bb70c7f9-1ed0-4e71-8c35-0f2c64824656%40googlegroups.com.
Message has been deleted

John Ivanoff

unread,
Jan 15, 2018, 3:15:27 PM1/15/18
to Ruby on Rails: Talk
I use
/app/views/layouts/application.html.erb

<html>
  <head>
  <%= yield :head %>
  </head>
  <body>
  <%= yield %>
  </body>
</html>

/app/views/foo/bar.html.erb

<% content_for :head do %>
  <title>A simple page</title>
<% end %>
 
<p>Hello, Rails!</p>

This way I can add "'meta descriptions" and "meta keywords" to individual pages

HTH
John

Robert Phillips

unread,
Jan 18, 2018, 2:38:56 AM1/18/18
to Ruby on Rails: Talk
thanks all, I will bear all this in mind.
Reply all
Reply to author
Forward
0 new messages