現在、railsでcocoonとprawnを使ってpdfに出力する帳票を作成しています。
入力フォームで登録した値をpdfで表示したいです。
cocoonで動的な入力フォームは完成しており、prawnでpdfの出力はできている状態です。
親要素の値は全て表示できているのですが、その親要素のIDに紐づく複数の子要素の表示がうまくいきません。eachなどで繰り返し処理を試してみたりしましたが、うまくいきません。
何かいい方法があればよろしくお願いします。
エラーメッセージ
該当のソースコード
rails
1class CostPdfsController < ApplicationController 2 before_action :set_cost_pdf, only: [:edit, :update, :destroy] 3 before_action :authenticate_user! 4 before_action :move_to_index, only: [:edit] 5 6 def index 7 @cost_pdfs = CostPdf.where(user_id: current_user.id) 8 @search_cost = CostPdf.ransack(params[:q]) 9 @cost_pdfs = @search_cost.result(distinct: true).order('created_at DESC') 10 end 11 12 def new 13 @cost_pdf = CostPdf.new 14 @quotation = @cost_pdf.quotations.build 15 end 16 17 def create 18 @cost_pdf = CostPdf.new(cost_pdf_params) 19 if @cost_pdf.save 20 redirect_to cost_pdfs_path 21 else 22 render :new 23 end 24 end 25 26 def edit 27 end 28 29 def update 30 if @cost_pdf.update(cost_pdf_params) 31 redirect_to cost_pdfs_path 32 else 33 render :edit 34 end 35 end 36 37 def destroy 38 @cost_pdf.destroy 39 redirect_to cost_pdfs_path 40 end 41 42 def prawn 43 @cost_pdf = CostPdf.find(params[:id]) 44 # binding.pry 45 @quotations = @cost_pdf.quotations.where(cost_pdf_id:params[:id]) 46 respond_to do |format| 47 format.html 48 format.pdf do 49 cost_pdf = PracticePdf::CostPdf.new(@cost_pdf, @quotations) 50 send_data cost_pdf.render, 51 filename: 'cost_pdf.pdf', 52 type: 'application/pdf', 53 disposition: 'inline' 54 end 55 end 56 end 57 58 private 59 60 def cost_pdf_params 61 params.require(:cost_pdf).permit(:company_name, :postal_code, :address, :tell, :fax, :delivery_date, 62 :delivery_location, :payment_terms, :expiration_date, :subtotal, :tax, 63 :total, :cliant_name, :memo, quotations_attributes:[:id, :product_name, 64 :quantity, :unit, :unit_price, :money, :remarks, :_destroy]).merge(user_id: current_user.id) 65 end 66 67 def set_cost_pdf 68 @cost_pdf = CostPdf.find(params[:id]) 69 end 70 71 def move_to_index 72 unless user_signed_in? && current_user.id == @cost_pdf.user_id 73 redirect_to user_session_path 74 end 75 end 76 77end 78
ruby
1module PracticePdf 2 class CostPdf < Prawn::Document 3 def initialize(cost_pdf, quotation) 4 super(page_size: 'A4') 5 stroke_axis 6 7 ipaexm_path = "#{Rails.root}/app/assets/fonts/ipaexm.ttf" 8 ipapm_spec = {file: ipaexm_path, font: 'IPAPMincho'} 9 font_families.update("ipapm" => {normal: ipapm_spec, 10 bold: ipapm_spec, 11 italic: ipapm_spec, 12 bold_italic: ipapm_spec}) 13 font "ipapm" 14 @cost_pdf = cost_pdf 15 @quotation = quotation 16 create_title 17 end 18 def create_title 19 text '御 見 積 書', size: 20, align: :center 20 stroke_horizontal_line 180, 340, :at=> 750 21font_size = 16 22 move_down 55 23 text_box("#{Time.now.strftime("%Y年%m月%d日")}", 24 :at => [0, y], 25 :width => bounds.right, 26 :align => :right) 27 bounding_box([70, 680], width: 310, height: 65) do 28 text @cost_pdf.cliant_name, size: 12, align: :left 29 end 30 bounding_box([200, 680], width: 310, height: 65) do 31 text "御中", size: 12, align: :left 32 end 33 bounding_box([33, 650], width: 310, height: 65) do 34 text "下記の通り御見積り申し上げます。", size: 12, align: :left 35 end 36 bounding_box([40, 620], width: 310, height: 65) do 37 text "1.受渡期日 #{@cost_pdf.delivery_date}", size: 12, align: :left 38 end 39 bounding_box([40, 600], width: 310, height: 65) do 40 text "2.納入場所 #{@cost_pdf.delivery_location}", size: 12, align: :left 41 end 42 bounding_box([40, 580], width: 310, height: 65) do 43 text "3.支払条件 #{@cost_pdf.payment_terms}", size: 12, align: :left 44 end 45 bounding_box([40, 560], width: 310, height: 65) do 46 text "4.有効期限 #{@cost_pdf.expiration_date}", size: 12, align: :left 47 end 48 bounding_box([305, 693], width: 310, height: 65) do 49 text "〒#{@cost_pdf.postal_code}", size: 12, align: :left 50 end 51 bounding_box([308, 680], width: 310, height: 65) do 52 text @cost_pdf.address, size: 12, align: :left 53 end 54 bounding_box([308, 665], width: 310, height: 65) do 55 text @cost_pdf.company_name, size: 12, align: :left 56 end 57 bounding_box([308, 648], width: 310, height: 65) do 58 text "TEL#{@cost_pdf.tell}/FAX#{@cost_pdf.fax}", size: 12, align: :left 59 end 60 bounding_box([390,610], :width=>270,:height=>360){ 61 table [ 62 ["検 印","担当者印"] 63 ] 64 } 65 bounding_box([89, 535], width: 310, height: 65) do 66 move_down 10 67 text "御 見 積 金 額 ", size: 16, align: :left 68 end 69 bounding_box([135, 535], width: 310, height: 65) do 70 move_down 10 71 text "¥#{@cost_pdf.total.to_s(:delimited)}- (消費税込)", size: 16, align: :right 72 end 73 74 bounding_box([89, 535], width: 310, height: 65) do 75 move_down 10 76 @quotation.each do |quotation| 77 text quotation.product_name, size: 16, align: :left 78 end 79 end 80 end 81 bounding_box([6.5, 500], width: 510, height: 65) do 82 rows = [["品 名","数 量","単位","単 価","金 額","備 考"], 83 84 table(rows, column_widths: [200, 45, 35, 60, 80, 90], position: :center) do |table| 85 table.cells.size = 11 86 table.row(0).align = :center 87 end 88 bounding_box([120, 135], width: 310, height: 65) do 89 text "小 計", size: 12, align: :left 90 end 91 bounding_box([100, 135], width: 310, height: 65) do 92 text "#{@cost_pdf.subtotal.to_s(:delimited)}", size: 12, align: :right 93 end 94 bounding_box([120, 115], width: 310, height: 65) do 95 text "消 費 税 等 ( 10% )", size: 12, align: :left 96 end 97 bounding_box([100, 115], width: 310, height: 65) do 98 text "#{@cost_pdf.tax.to_s(:delimited)}", size: 12, align: :right 99 end 100 bounding_box([120, 95], width: 310, height: 65) do 101 text "合 計", size: 12, align: :left 102 end 103 bounding_box([100, 95], width: 310, height: 65) do 104 text "#{@cost_pdf.total.to_s(:delimited)}", size: 12, align: :right 105 end 106 bounding_box([10, 75], width: 310, height: 65) do 107 text "備 考:", size: 12, align: :left 108 end 109 bounding_box([10, 63], width: 310, height: 65) do 110 text @cost_pdf.memo, size: 12, align: :left 111 112 end 113 114 end 115 end 116end
rails
1<div class="nested-fields"> 2 <div class="form-block" id="form_block[0]"> 3 <div class="row"> 4 <div class="col-md-6 mb-3"> 5 <label for="familyName">品名</label> 6 <%= f.text_field :product_name, class:"form-control", placeholder:"必須" %> 7 <label for="familyName">数量</label> 8 <%= f.text_field :quantity, class:"form-control", placeholder:"任意" %> 9 <label for="givenName">単位</label> 10 <%= f.text_field :unit, class:"form-control", placeholder:"任意" %> 11 </div> 12 <div class="col-md-6 mb-3"> 13 <label for="givenName">単価</label> 14 <%= f.text_field :unit_price, class:"form-control", placeholder:"任意" %> 15 <label for="givenName">金額</label> 16 <%= f.text_field :money, class:"form-control", id:"money-cost[0]", placeholder:"必須" %> 17 <label for="familyName">備考</label> 18 <%= f.text_field :remarks, class:"form-control", placeholder:"任意" %> 19 </div> 20 </div> 21 <%= link_to_remove_association "削除", f %> 22 </div> 23 </div> 24
試したこと
@quotations = @cost_pdf.quotations.where(cost_pdf_id:params[:id])
↑
ここに値が入っているのは binding.pryで確認できています。
@quotation.each do |quotation|
quotation.product_neme
end
これは間違っているでしょうか?
補足情報(FW/ツールのバージョンなど)
ruby 2.6.5 rails 6.0.0

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/01/16 03:42
2022/01/16 23:58
2022/01/17 04:46
2022/01/17 05:13
2022/01/17 05:47
2022/01/17 12:56
2022/01/17 13:23