简体   繁体   中英

about layouts in ruby on rails tutorial

I'm studying Ruby on Rails by Ruby on Rails Tutorial. 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

It seems there full_title has not been defined.

Just write

@full_title = "Ruby on Rails Tutorial Sample App"

Hope this help you !!!

Looks like the method full_title is not defined.

Make sure that it's properly defined in app/helpers/application_helper.rb .

Here is my app/helpers/application_helper.rb file:

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:

<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 "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.)

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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