简体   繁体   English

关于Ruby on Rails布局的教程

[英]about layouts in ruby on rails tutorial

I'm studying Ruby on Rails by Ruby on Rails Tutorial. 我正在研究Ruby on Rails教程中的Ruby on Rails。 I did test but, I found error. 我做了测试,但是发现错误。 Where is wrong? 哪里错了?

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>'

I'm not damn sure but Its my assumption that you would be following Ch 3- Testing Titles 我不确定,但我认为您会遵循第3章测试标题

It seems there full_title has not been defined. 似乎尚未定义full_title

Just write 写吧

@full_title = "Ruby on Rails Tutorial Sample App"

Hope this help you !!! 希望这对您有帮助!

Looks like the method full_title is not defined. 看起来方法full_title尚未定义。

Make sure that it's properly defined in app/helpers/application_helper.rb . 确保在app/helpers/application_helper.rb正确定义了它。

Here is my app/helpers/application_helper.rb file: 这是我的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

And don't forget to edit app/views/layouts/application.html.erb file as well: 并且不要忘记同时编辑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> 

Per the implementaion of full_title method, it returns either base_title (ie the string Ruby on Rails Tutorial Sample App ), or "The title of your choice" | Ruby on Rails Tutorial Sample App 根据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 if you pass in the title as an argument to the method, which you can do by putting <% provide :title, "your title here" %> on each page.) "The title of your choice" | Ruby on Rails Tutorial Sample App如果您将标题作为方法的参数传递的话,可以通过在每个页面上放置<% provide :title, "your title here" %>来实现)。

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM