2012-04-23 18 views
0

我想要做的就是創建一個HTML頁面,其中包含與該文本關聯的鏈接和名稱/文本列表。如何將錨文本添加到我正在使用Nokogiri構建的HTML文檔中的鏈接?

E.g. <a href="www.google.com">Google</a>

我可以將Google更改爲我想要的任何文本(包括來自變量的數據)。

我有這樣的:

builder = Nokogiri::HTML::Builder.new do |doc| 
    doc.html { 
     doc.body { 
      contents.each do |i| 
       doc.p { 
        doc.a(:href => list.first) 
        } 
      end   
      }   
     } 
end 

這只是產生這樣的:

<html><body><p><a href="someurl.com"></a></p></body></html> 

我想要的是,雖然是什麼:

<html><body><p><a href="someurl.com">First Link</a></p></body></html> 

我該怎麼做,在引入nokogiri ?

謝謝。

回答

3
doc.a 'text_goes_here', :href => 'href_goes_here' 
1

其實......只是想通了。我只需要加doc.text "First Link"

所以更新的代碼段是這樣的:

builder = Nokogiri::HTML::Builder.new do |doc| 
    doc.html { 
     doc.body { 
      contents.each do |i| 
       doc.p { 
        doc.a(:href => list.first) { 
          doc.text "First Link" 
         } 
        } 
      end   
      }   
     } 
end 

就像一個魅力。

相關問題