顧客一覧XMLを取得するアクションを作成
> ruby script/generate controller Accounting
app/controllers/accounting_controller.rbに以下のメソッドを追加
def customer_list @customers = Customer.find(:all) end
app/views/accounting/customer_list.rxmlを以下のように作成
xml.instruct! xml.customer_list_result do xml.invoice_customers do @customers.each do |customer| xml << customer.to_xml(:dasherize=>false, :skip_instruct=>true, :only=>[:id,:name,:address], :root=>"customer", :include => :customer_type) # belongs_toで指定したリレーションもXMLに end end end
WEBrickサーバを起動し
> ruby script/server
次のようにブラウザに表示されたらOK
このXMLは保存して(customer_list.xmlとしておく)、次のiReportのテンプレートして使う。
最終的にはこのXMLの内容がPDFとして出力される。
<?xml version="1.0" encoding="UTF-8"?> <customer_list_result> <invoice_customers> <customer> <address>東京都渋谷区</address> <id type="integer">1</id> <name>株式会社 ABC</name> <customer_type> <id type="integer">1</id> <name>支払先</name> </customer_type> </customer> <customer> <address>東京都渋谷区</address> <id type="integer">2</id> <name>株式会社 CDE</name> <customer_type> <id type="integer">1</id> <name>支払先</name> </customer_type> </customer> <customer> <address>東京都渋谷区</address> <id type="integer">3</id> <name>株式会社 EFG</name> <customer_type> <id type="integer">3</id> <name>支払・請求</name> </customer_type> </customer> <customer> <address>東京都渋谷区</address> <id type="integer">4</id> <name>XYZ 株式会社</name> <customer_type> <id type="integer">2</id> <name>請求先</name> </customer_type> </customer> </invoice_customers> </customer_list_result>