2009-06-18 11 views
-1

我有一個名爲每頁個人用戶的PDF動作,它有兩個模式,即從數據庫retrives信息的用戶模式通信,但只有所有用戶都具有特定的信息,然後它將信息發送給模型報告,然後當我檢查每個用戶的屬性時,它顯示的方式是我沒有從模型用戶調用它,它需要每對並把它放在一個不同的順序,所以當我寫信息到PDF上的表格它顯示它不好,像首字母縮寫可能是我的表的最後一個記錄和手機可能是第一個..我必須創建一個新的對於每一個用戶的信息頁面..哈希值之前,我的一個PDF打印出來的表格裏面我怎麼能進行排序

我的報告的代碼看起來的一樣:

def self.generate_individual_pdf(people,residents,user,estate = nil, title = nil)

pdf = PDF::Writer.new 
title = !title ? "Report" :title 
pdf.select_font "Times-Roman" 
pdf.text "<em>Compiled By: #{user.presentation_name}</em> <em>Estate: #{estate.name}</em>          <em>Created On: #{Time.now.strftime('%d/%m/%Y %H:%M')}</em>", :justification => :center 
    x = 0 
25.times do 
    pdf.text ""+"      " 
    x+=1 
    puts x 
end 
pdf.text "<em>User Report For: #{estate.name}</em> ", :font_size => 18, :bold => true, :justification => :center 
if estate.logo 
    pdf.image "#{RAILS_ROOT}/public"+estate.logo.public_filename,:width => 100, :height => 100, :resize => 1.1, :justification => :center 
end 
y = 0 
#loop people to create table for every person 
people.each do |p| 
    pdf.start_new_page 
    pdf.text"     "+p.Title.to_s+" "+p.Firstname.to_s+" "+p.Lastname.to_s, :font_size => 12, :justification => :center 
    pdf.text ""+"      " 
    pdf.text ""+"      " 
    pdf.text "Please verify if your details below are correct, and rectify them in the third column of the table.", :font_size => 8 
    pdf.text ""+"      " 
    table = PDF::SimpleTable.new 
    table.column_order.push(*["head1","head2","head3"]) 
    headings = ["head1","head2","head3"] 
    headings.each do |h| 
    table.columns[h] = PDF::SimpleTable::Column.new(h) 
    table.columns[h].heading = h.humanize 
    end 
    #display tables 
    table.show_headings = false 
    table.orientation = :right 
    table.position = :left 
    data = [] 
    p.attributes.each_pair do |h| 
    data << { "head1" => h[0], "head2" => h[1], "head3" => "        "} 
    end 


    table.data.replace data 
    table.render_on(pdf) 

    i = 0 
    28.times do 
    pdf.text ""+"      " 
    i+=1 
    puts i 
    end 
    pdf.text "Kind Regards"+ "     "+p.Title.to_s+" "+p.Firstname.to_s+" "+p.Lastname.to_s, :font_size => 11 
    pdf.text ""+"      " 
    pdf.text "Signature       "+"......................................." 

end 

那麼它產生的輸出,當它循環throught的p.attribute.each_pair做| H | 數據< < { 「頭像1」=> H [0], 「HEAD2」=> H [1], 「頭像3」=> 「這應該是零」} 端

請人我一直試圖使用所有這種排序,但我只是不能得到正確的答案..任何人可以幫助請隨時..感謝提前。

+0

句子以大寫字母開頭,結尾只有一個'.`字符。 – 2012-01-27 02:20:55

回答

0

散列紅寶石本身是無序對象。你不能保證獲取數據了出來在你把它放在相同的順序。

這聽起來像對屬性的隨機順序出來(或者至少不是你想要的順序)。對付這是一種可能的方式...

p.attributes.keys.sort do |key| 
    data << { "head1" => key, "head2" => p.attributes[key], "head3" => nil } 
end 

這會給你回的值名稱

+1

僅供參考,如紅寶石1.9,散列保持它們的順序:http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l86 這是不相關的這種情況下,必然的,但它是很好的瞭解。 – Telemachus 2009-06-18 12:39:48

相關問題