문제

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?

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top