2009-06-18 4 views
-1

ページごとに個別のユーザーpdfというアクションがあり、データベースから情報を取得するユーザーモデルの2つのモデルと通信しますがすべてのユーザーのための特定の情報のみを取得すると、情報を取得してモデルレポートに送信します。モデルユーザーから呼び出されていない方法で表示される各ユーザーの属性を調べると、別の順序でそれを配置するので、私はそれが悪い、イニシャルが私のテーブルの最後のレコードかもしれないと、携帯電話が最初のものかもしれない..私は、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 

それを行うp.attribute.each_pair throughtループするとき、それは出力を生成します| H | データ< < { "HEAD1" => H [0]、 "HEAD2" => H [1]、 "head3" => "これは nilをなければならない"} 端私がされている

ください人この種の並べ替えのすべてを使用しようとしているが、私はちょうど正しい答えを得ることはできません。

+0

文は大文字で始まり、1つだけで終わり、 '.'文字で終わります。 –

答えて

0

ルビのハッシュは、本質的に順序付けられていないオブジェクトです。

属性のペアがランダムな順序(または少なくともあなたが望む順序ではない)で出てくるように聞こえます。これに対処する1つの可能な方法はある...

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

これはRuby 1.9のすべての属性は、値の名前のアルファベット順にソート

+1

参考資料Ruby 1.9以降、ハッシュはその順序を保持しています:http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l86 これは必ずしもこのケースには関係ありませんが、知っておきましょう。 – Telemachus

+0

それでも私にはエラーが出ます:#の未定義メソッド '> ' –

0

あなたをバック与えるあなたが1.8である場合が注文したハッシュを含んでいます、その後取りますルビーファセットから辞書クラスを見てください。 (http://facets.rubyforge.org/doc/api/more/classes/Dictionary.html

これは過去にこれを行うのに役立ちました。

関連する問題