2012-12-05 43 views
1

在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:

  1. 刪除:

    <% 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「

任何建議表示讚賞。

+0

作爲一個快速評論,你應該在控制器中設置一些值@full_title = ___。 然後,你可以做額外的調節(仍然在控制器中),例如,如果有一個標題可以連接條和標題。 – wrhall

+0

@whall我明白這一點。我已經嘗試過@full_title和:full_title無濟於事。我也嘗試過使用'<%= yield(@full_title)%>'而不是'<%= print @full_title%>' – Chiperific

+0

爲什麼你回滾編輯來改進你的文章? – Sam

回答

5

這是怎麼回事?

module ApplicationHelper 

    # Returns the full title on a per-page basis. 
    def full_title(page_title) 
    base_title = "Ruby on Rails Tutorial Sample App" 
    if page_title.empty? 
     base_title 
    else 
     "#{base_title} | #{page_title}" 
    end 
    end 
end 

http://ruby.railstutorial.org/chapters/rails-flavored-ruby#code-title_helper

只需撥打您的視圖中的方法。 所以它應該像 <title><%= full_title(yield(:title)) %></title> 像他說的。

+1

哇。我通過第3章和第4章進行了回顧,並以某種方式錯過了這一點。非常感謝! – Chiperific

相關問題