I am running through the Rails Tutorial by Michael Hartl (Screen Cast). Ran into the and issue in chapter 4 on the title helper.
I have been putting my own twist on the code as I go to make sure I understand it all. However on this one I it is very similar and I am not quite sure why it is acting the way it is.
Here is the code:
Application.html.erb
<!DOCTYPE html>
<html>
<head>
<%- Rails.logger.info("Testing: #{yield(:title)}") %>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
Application_helper.rb
module ApplicationHelper
def full_title(page_title)
full_title = "Ruby on Rails Tutorial App"
full_title += " | #{page_title}" unless page_title.blank?
end
end
Home.html.erb
<h1><%= t(:sample_app) %></h1>
<p>
This is the home page for the <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a> sample application
</p>
about.html.erb
<% provide(:title, t(:about_us)) %>
<h1><%= t(:about_us) %></h1>
<p>
The <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a> is a project to make a book and screencast to teach web development with <a href="http://railstutorial.org/">Ruby on Rails</a>. This is the sample application for the tutorial.
</p>
What Happens: The code works fine when I set the provide method like on the about page. However when I do not it does not seem to even call the helper. I am assuming that because no title is passed back. Any ideas on what I am doing wrong?
Thank you all for your help.
Edit:
This is the fix. Thanks to Paul for pointing out that the last line was returning nothing and was why my code was not working. Changed it so it return something not mater the case.
Application_helper.rb
module ApplicationHelper
def full_title(page_title)
full_title = "Ruby on Rails Tutorial App"
page_title.present? ? (full_title += " | #{page_title}") : full_title
end
end
I seems you have an issue with the last line of your
full_titlehelper method as it doesn’t return anything if thepage_titleis blank (all that happens is that local variablefull_titlegets assigned). Try changing it back to how it is written in the book and see if it works.