3

I have a layout and partials that are working fine. The problem is different models all have the same title. If would be nice if I can customize the title for each model.

This is my layout:

<html>
<head>

    <title><%= content_for :title %></title>

</head>
<body>

    <%= render 'header' %>
    <%= render "sidebar" %>
    <%= render "content" %>
    <%= render template: "layouts/footer" %>

</body>
</html>

This is my controller I tried making work based on undefined method `content_for' in presenter rails:

class ViewerController < ApplicationController

   def index

        helpers.content_for :title, "Viewer"
        #helpers.content_for (:title)  "Viewer" #doesn't work

   end

    def helpers 

        ActionController::Base.helpers      

    end

end

I tried changing the layout to <%= helpers.content_for :title %> but it did not work. I also tried changing the controller to helpers.content_for (:title) "Viewer" and elpers.content_for (:title) || "Viewer" based on other posts but they fail.

If this approach does not work then I am open to another.

Community
  • 1
  • 1
Joe
  • 379
  • 1
  • 6
  • 21
  • I've found if I have a single line in index.html.erb with the code "<% provide(:title, "Viewer") %>" it does work. I didn't know a html.erb file will execute even if its not being displayed in the layout with <%= yield %>. Is this design clean? I will post it as the solution unless someone points out something better. – Joe Jul 22 '15 at 20:08
  • Possible duplicate of [Rails: How to change the title of a page?](https://stackoverflow.com/questions/185965/rails-how-to-change-the-title-of-a-page) – Rajkaran Mishra Sep 06 '18 at 11:08

3 Answers3

4

I figured it out. Here is my layout:

<!DOCTYPE HTML>

<html>
<head>

    <title><%= yield :title %></title>

</head>
<body>

    <%= render 'header' %>
    <%= render "sidebar" %>    
    <%= render "content" %>
    <%= render template: "layouts/footer" %>

</body>

Here is my view (index.html.erb):

<% provide(:title, "Viewer") %>

I did not know the erb will execute even if it is not called to be displayed anywhere. I do not have a <%= yeild %> anywhere. I can put HTML in there and no one will ever see it. But the ruby code will execute.

Joe
  • 379
  • 1
  • 6
  • 21
1

In application_helper.rb add the following code:

def full_title(page_title = '')                    
  base_title = "Ruby on Rails Tutorial Sample App" 
  if page_title.empty?                             
    base_title                                     
  else
    page_title + " | " + base_title
  end                 
end

Add following code in application.html.erb file:

<title><%= full_title(@title) %></title>

And in controller you can then assign value to instance variable like this on any of the routes methods e.g. index:

@title = "Home"

But it is not a good idea to use an instance variable to show the title.

misterhtmlcss
  • 363
  • 1
  • 16
  • This does not work since there are no individual pages, just the layout. I tried provide(:title, "Home") in the controller but it does not work. – Joe Jul 21 '15 at 18:47
  • provide(:title, "Home") wont work in controller, it is for view only. If you want to set the title from the controller you have to use instance variable – Prosenjit Saha Jul 22 '15 at 05:11
  • OK, so how do I make an instance variable to just display the title? – Joe Jul 22 '15 at 20:07
0

Your layout should be yield :title instead of content_for :title. In this way, for your controllers you can just do as you are already doing correctly, and that is setting the content for the layout to be their respective titles.

With yield: title you are practically doing the same thing as render, except it is handled by the other controllers/views/models.

  • yield: title does not work without <% provide(:title, "Home") %>. And provide(:title, "Home") does not work in the controller. – Joe Jul 21 '15 at 18:49