簡體   English   中英

關於Ruby on Rails布局的教程

[英]about layouts in ruby on rails tutorial

我正在研究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>'

我不確定,但我認為您會遵循第3章測試標題

似乎尚未定義full_title

寫吧

@full_title = "Ruby on Rails Tutorial Sample App"

希望這對您有幫助!

看起來方法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 "The title of your choice" | Ruby on Rails Tutorial Sample App如果您將標題作為方法的參數傳遞的話,可以通過在每個頁面上放置<% provide :title, "your title here" %>來實現)。

希望能幫助到你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM