Question

I am getting the following error:

undefined method `full_title'

On this line:

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

On my layouts file:

<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title>
    <%= stylesheet_link_tag "application", media: "all",
                                           "data-turbolinks-track" => true %>
    <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
    <%= csrf_meta_tags %>
  </head>
  <body>
    <%= render 'layouts/header' %>
    <div class="container">
      <%= yield %>
      <%= render 'layouts/footer' %>
    </div>
  </body>
</html>

I am trying to do something similar to Mike Hartle rails tutorial with the page titles except I am not using tests. So I have not created a support file in the spec folder. I actually have no spec folder. I believe not having the support file with this code:

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

Is causing the error. What is the proper way to fix this is you do not want to create tests and hence don't want a spec folder? Where can I put this code?

Was it helpful?

Solution

Any method accessible to the views directly has to go to the helper.

Since you are trying to access this method in your layouts, put your code in the application_helper.rb file.

All helpers are modules only.

If you don't have the file, create one in app/helpers

module ApplicationHelper
 def full_title(page_title)
  base_title = "Ruby on Rails Tutorial Sample App"
  if page_title.empty?
    base_title
  else
    "#{base_title} | #{page_title}"
  end
 end
end

Then, include ApplicationHelper in application_controller.rb

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top