在Michael Hartl的RoR教程中:第5章末尾的練習涉及簡化RSpec測試。M Hartl的Ruby on Rails教程第5章主頁上的自定義標題
5.37(http://ruby.railstutorial.org/chapters/filling-in-the-layout#sec-layout_exercises)定義了主頁的幾個測試。 在文件規範/助理/ application_helper_spec.rb:
require 'spec_helper'
describe ApplicationHelper do
describe "full_title" do
.
.
.
.
it "should not include a bar for the home page" do
full_title("").should_not =~ /\|/
end
end
end
對於這個測試通過,我需要有主頁標題顯示: 「Ruby on Rails的教程示例應用程序」 而不是「的Ruby on Rails教程示例應用程序|主頁「
但是,本教程並未引導我如何編碼該更改。
這是我已經試過:
在應用程序/視圖/ static_pages/home.html.erb:
刪除:
<% provide(:title, 'Home') %>
在app/views/layouts/application.html.erb 將標籤編輯爲:
<title>
<% if :title
full_title = "Ruby on Rails Tutorial Sample App | " && yield(:title)
else
full_title = "Ruby on Rails Tutorial Sample App"
end %>
<%= print full_title %>
</title>
和其他變化我小白腦可能召集。 這裏就是我試圖完成:
如果頁面沒有提供標題,則返回「Ruby on Rails的教程示例應用程序」
如果有Ruby on Rails的教程示例應用程序提供,返回」標題| #PageTitle「
任何建議表示讚賞。
作爲一個快速評論,你應該在控制器中設置一些值@full_title = ___。 然後,你可以做額外的調節(仍然在控制器中),例如,如果有一個標題可以連接條和標題。 – wrhall
@whall我明白這一點。我已經嘗試過@full_title和:full_title無濟於事。我也嘗試過使用'<%= yield(@full_title)%>'而不是'<%= print @full_title%>' – Chiperific
爲什麼你回滾編輯來改進你的文章? – Sam