关于ruby on rails教程中的布局

时间:2015-12-27 11:01:23

标签: ruby-on-rails ruby railstutorial.org

我正在通过Ruby on Rails教程学习Ruby on Rails。 我做了测试但是,我发现了错误。 哪里错了?

test_should_get_home#StaticPagesControllerTest (1451158535.88s)
ActionView::Template::Error:         ActionView::Template::Error: undefined method `full_title' for #<#<Class:0x007ff2da1e0bc0>:0x007ff2da1dbeb8>
            app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb__2067729572228884173_70340508945500'
            test/controllers/static_pages_controller_test.rb:9:in `block in <class:StaticPagesControllerTest>'
        app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb__2067729572228884173_70340508945500'
        test/controllers/static_pages_controller_test.rb:9:in `block in <class:StaticPagesControllerTest>'

2 个答案:

答案 0 :(得分:0)

我不是很确定,但我认为你会关注Ch 3- Testing Titles

似乎full_title尚未定义。

只需写下

@full_title = "Ruby on Rails Tutorial Sample App"

希望这能帮到你!!!

答案 1 :(得分:0)

看起来方法full_title未定义。

确保在app/helpers/application_helper.rb

中正确定义了它

这是我的app/helpers/application_helper.rb文件:

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

并且不要忘记编辑app/views/layouts/application.html.erb文件:

<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 %>
  <%= render "layouts/shim" %>
</head> 

根据full_title方法的实现,如果您将标题作为参数传递给base_title,则返回Ruby on Rails Tutorial Sample App(即字符串"The title of your choice" | Ruby on Rails Tutorial Sample App)或<% provide :title, "your title here" %>方法,您可以通过在每个页面上放置{{1}}来完成。)

希望它有所帮助!