ruby 的多行註解
==begin
This is a comment line
it explains that the next line of code displays
a welcome message
==end
從 xml 檔讀取資料,寫入資料表
require 'rubygems'
require 'xmlsimple'
xmlfile = "file.xml"
url = "http://ma.fiterl.net/upload/#{xmlfile}"
Tracking.connection.execute("TRUNCATE TABLE books")
xml_data = Net::HTTP.get_response(URI.parse(url)).body
data = XmlSimple.xml_in(xml_data)
data['item'].each do |item|
book = Book.new
item.sort.each do |k, v|
book[k] = v.to_s
end
book.save
end
這樣就把一筆一筆(以 item 為節點)寫入資料表了。
假定標簽名同於欄位名。
中文字串的處理:
用 split 切割字串,中文時會取得亂碼。加上(//u) 可確認為 unicode 處理,便可解決。
<%= link_to n.title.split(//u)[0..20].to_s + "…", :controller => "note", :action => "show", :id => n.id %><br/>
用字串來組成物件
eval("t." + t.trackable_type + ".subject") => t.question.subject