【问题标题】:about layouts in ruby on rails tutorial关于 ruby on rails 教程中的布局
【发布时间】:2015-12-27 11:01:22
【问题描述】:
我正在通过 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>'
【问题讨论】:
标签:
ruby-on-rails
ruby
railstutorial.org
【解决方案1】:
我不太确定,但我假设你会关注Ch 3- Testing Titles
那里full_title好像还没有定义。
随便写
@full_title = "Ruby on Rails Tutorial Sample App"
希望对你有帮助!!!
【解决方案2】:
看起来方法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" %> 在每一页上。)
希望对你有帮助!