0

Following this tutorial (https://www.railstutorial.org/book/rails_flavored_ruby), I wish to vary the title of a layout based on the controller.

I have my page in four sections which I can change at will. It is working well excpet I am having a hard time changing the title.

Here is my layout:

<html>
<head>

    <%= stylesheet_link_tag "ERP" %>

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

</head>
<body>

    <%= render 'header' %>
    <%= render "sidebar" %>
    <%= render "content" %>
    <%= render "footer" %>

</body>
</html>

I tried to add the title in the controller but it did not work:

class ViewerController < ApplicationController

   def index

        provide(:title, "Viewer")

   end

   def update

   end

end

I get the error "undefined method `provide' for #". We will have more apps that use the same layout, changing what sections they need to. I would like to change the title based on the app.

Anyone know how I can use the provide function in a controller?

Joe
  • 379
  • 1
  • 6
  • 21

1 Answers1

2

I believe what you're looking for in your controller is not provide but:

content_for :title, 'Viewer' 

API Docs content_for v. provide

craig.kaminsky
  • 5,588
  • 28
  • 31
  • Thanks for your reply. Unfortunately I tried 'content_for :title, 'Viewer'' and received "undefined method `content_for' " Then I searched but could not find any examples other than the one you posted. So I used the alternate syntax, not knowing what is wrong, 'content_for(:title) || "Viewer"' but this failed with the same error. Tried this: http://stackoverflow.com/questions/30116785/undefined-method-content-for-in-presenter-rails although it does not make an error :title does not get set. Is there an alternate method to accomplish this goal? ' – Joe Jul 21 '15 at 14:09