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

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

then in the page
/app/views/fo/bar.html.erb

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

This way I can add other tags to the head section. Like `meta description` 
and maybe `meta keywords` for individual pages.
Hassan mention this link 
earlier. 
http://guides.rubyonrails.org/layouts_and_rendering.html#using-the-content-for-method

HTH

John

On Sunday, January 14, 2018 at 9:41:17 AM UTC-5, Walter Lee Davis wrote:
>
>
> > On Jan 13, 2018, at 11:01 PM, Robert Phillips <robert.p...@gmail.com 
> <javascript:>> wrote: 
> > 
> > 
> > 
> > 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? 
> > 
>
> 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 <javascript:>. 
> > To post to this group, send email to rubyonra...@googlegroups.com 
> <javascript:>. 
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/rubyonrails-talk/bb70c7f9-1ed0-4e71-8c35-0f2c64824656%40googlegroups.com.
>  
>
> > For more options, visit https://groups.google.com/d/optout. 
>
>

-- 
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-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/b984fbd6-ea8f-49d1-95aa-8032d15ca0c3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to