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

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 asdf it doesn’t change the title.

Even if I do

asdf

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

aaaaa

What gets rendered is this

Test15 aaaaa

So, what it is doing is rails is taking its own template then it dumps my template into its

So the title isn’t getting set

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

This: Layouts and Rendering in Rails — Ruby on Rails Guides 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!

Re: [Rails] I can’t set the title of a page no matter what I have tried to put in my .html.erb file 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

Saturday, January 13, 2018, 5:18:23 PM, you wrote:

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 asdf it doesn’t change the title.

Even if I do

asdf

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

aaaaa

What gets rendered is this

Test15 aaaaa

So, what it is doing is rails is taking its own template then it dumps my template into its

So the title isn’t getting set

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

<%= yield %>

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?

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

> 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: Layouts and Rendering in Rails — Ruby on Rails Guides 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?

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

I use

/app/views/layouts/application.html.erb

<%= yield :head %>

<%= yield %>

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

<% content_for :head do %>

A simple page

<% end %>

Hello, Rails!

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

As Hassan mentioned Layouts and Rendering in Rails — Ruby on Rails Guides

HTH

John

thanks all, I will bear all this in mind.