2011-06-19 46 views
7

我對Ruby on Rails很新,甚至對Haml更新,我在2個小時前開始使用它。所以我正在遵循Ruby on Rails教程,並決定在視圖上使用Haml,但我不確定這是否是顯示事物的正確方法(感覺有點奇怪)。有人能啓發我嗎? :)這是使用Haml顯示/連接文本的正確方法嗎?

%h1= "About Us" 
%p 
    =link_to 'Ruby on Rails Tutorial','http://railstutorial.org' 
    &= 'is a project to make a book and screencasts to teach web development with' 
    &= link_to 'Ruby on Rails', 'http://rubyonrails.org' 
    &= '. This is the sample application for the tutorial.' 

我已經試過這太:

:ruby 
    first_link = link_to 'Ruby on Rails Tutorial','http://railstutorial.org' 
    second_link = link_to 'Ruby on Rails', 'http://rubyonrails.org' 

%h1= "About Us" 
%p 
    = first_link 
    &= 'is a project to make a book and screencasts to teach web development with' 
    &= second_link 
    &= '. This is the sample application for the tutorial.' 

回答

11

可以使用#{}文本塊內包裝Ruby代碼,如果你只是想文本(就像你%h1)你不需要使用=

%h1 About Us 
%p 
    #{link_to 'Ruby on Rails Tutorial','http://railstutorial.org'} is a project to make a book and screencasts to teach web development with #{link_to 'Ruby on Rails', 'http://rubyonrails.org'}. This is the sample application for the tutorial. 

如果你想打破了線,使其更容易在你的編輯處理,確保您縮進各行各等量,並且不以一個Ruby函數中拆分:

%h1 About Us 
%p 
    #{link_to 'Ruby on Rails Tutorial','http://railstutorial.org'} is a project to make a 
    book and screencasts to teach web development with #{link_to 'Ruby on Rails', 'http://rubyonrails.org'}. 
    This is the sample application for the tutorial. 
+0

完美答案,非常感謝。 :) –

相關問題