未定义的方法‘full_title’ [英] undefined method `full_title'

查看:38
本文介绍了未定义的方法‘full_title’的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:

未定义的方法`full_title'

在这一行:

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

在我的布局文件中:

<头><title><%= full_title(yield(:title)) %></title><%= stylesheet_link_tag "application", media: "all",数据涡轮链接轨道"=>真%><%= javascript_include_tag "application", "data-turbolinks-track" =>真%><%= csrf_meta_tags %><身体><%= 渲染布局/标题"%><div class="容器"><%=产率%><%= 渲染布局/页脚"%>

</html>

我正在尝试使用页面标题执行类似于 Mike Hartle rails 教程的操作,但我没有使用测试.所以我没有在 spec 文件夹中创建支持文件.我实际上没有规范文件夹.我相信没有包含此代码的支持文件:

def full_title(page_title)base_title = "Ruby on Rails 教程示例应用程序"如果 page_title.empty?base_title别的#{base_title} | #{page_title}"结尾结尾

导致错误.解决此问题的正确方法是您不想创建测试,因此不需要 spec 文件夹?我可以把这个代码放在哪里?

解决方案

任何可直接访问视图的方法都必须转到帮助程序.

由于您尝试在布局中访问此方法,请将您的代码放在 application_helper.rb 文件中.

所有的助手都是模块.

如果您没有该文件,请在 app/helpers

中创建一个

module ApplicationHelperdef full_title(page_title)base_title = "Ruby on Rails 教程示例应用程序"如果 page_title.empty?base_title别的#{base_title} | #{page_title}"结尾结尾结尾

然后,在 application_controller.rb 中include ApplicationHelper

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

这篇关于未定义的方法‘full_title’的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆