使用Rails 3从DB收集记录数组

时间:2015-06-04 10:32:12

标签: ruby ruby-on-rails-3.2

我想通过使用不同的ids Rails 3来访问DB中的数组,但是出现了以下错误。

  

错误:

NoMethodError in Payments#download_pdf

Showing C:/Site/swargadwara_puri1/app/views/payments/download_pdf.html.erb where line #110 raised:

undefined method `each' for #<PaymentVendor:0x329e9f8>
Extracted source (around line #110):

107:                                         <td style="width:50%; border-right: 1px solid black;">Item</td>
108:                                         <td style="width:20%; text-align:right; border-right: 1px solid black;">Amount</td>     
109:                                    </tr>
110:                                     <% @pdf_vendor_details.each do |details| %>
111:                                     <% @count=@count+1 %>
112:                                    <tr style="border: 1px solid black;">
113:                                         <td style="border-right: 1px solid black;"><%= @count %></td>

我的其他代码文件如下所示。

  

payments_controller.rb:

class PaymentsController < ApplicationController
    require "prawn/table"
    require "prawn"
    def payment
        @payment=Vendor.new
        @add_payment=AddPayment.new
        respond_to do |format|
            format.html 
            format.js
        end

    end
    def check_type  
        if params[:commit]=="submit"
            @vendor_type = PaymentVendor.where(:v_name => params[:v_name]).map{|v|[v.v_catagory ,v.Receipt_No]}
            #@vendor_type = PaymentVendor.where(:v_name => params[:v_name]).pluck(:v_catagory)
            @output=[]
            #@result=[]
            @r_no=[]
            @vendor_type.each do |i|
              if i.first == params[:payment][:s_catagory]
                @output << i[1]
              end  
            end  
            @output.each do |o|
                @r_no << o
                @result = PaymentVendor.where(:Receipt_No => @r_no,:v_catagory =>  params[:payment][:s_catagory],:v_name => params[:v_name],:v_status => "No" )
            end
               #puts "#{val} => #{index}" 
               #puts output1[index]
               #@result = PaymentVendor.where(:Receipt_No =>  @output[index],:v_catagory =>  params[:payment][:s_catagory],:v_name => params[:v_name] )

        else
            @v_name=Vendor.where(:s_catagory => params[:payment][:s_catagory] ).pluck(:v_name)
        end
    end
    def add_payment
        if params[:commit]
            #params[:details] == '1' ? remember : forget
            @userid=params[:deatils].collect{|id| id.to_i} if params[:deatils]
            #@userid=[]
            puts "id is #{@userid}"
            @info=0
            params[:infos].each do |info|
              @info=@info+info.to_i
            end
            @total_amount=params[:deatils]
            @total=0
            @count=0
            @delet_id=[]
            params[:deatils].each do |id|
                @delet_id << id
                @amount=PaymentVendor.find(id)
                @total=@total+@amount.v_amount.to_i
                @count=@count+1
            end
            if @total
                @outstanding= @info-@total
                if params[:add_payment][:p_catagory]=="Cash"
                @add_payment=AddPayment.create(:p_catagory => params[:add_payment][:p_catagory],:paid_amount => @total,:total_claim => @count,:outstanding => @outstanding )
                if @add_payment
                    @userid << @add_payment.id
                    @delet_id.each do |i|
                        @del_id=PaymentVendor.find(i)
                        @del_id.update_attributes(:v_status => "Yes")
                    end
                    @abc=true
                end
                end
                if params[:add_payment][:p_catagory]=="Cheque"
                    @add_payment=AddPayment.create(:p_catagory => params[:add_payment][:p_catagory],:paid_amount => @total,:total_claim => @count,:chk_details => params[:chk_details],:chk_no => params[:chk_no],:outstanding => @outstanding )
                    @userid << @add_payment.id
                    if @add_payment
                        @delet_id.each do |i|
                            @del_id=PaymentVendor.find(i)
                            @del_id.update_attributes(:v_status => "Yes")
                        end
                        @abc=true
                    end
                end
            end
        else
            if params[:add_payment][:p_catagory]=="Cheque"
                @chk=true
            else
                @Cash=true
            end
        end
    end
    def download_pdf
        @all_userid=params[:all_id]
        #puts "all ids are #{@all_userid}"
        #puts "rest ids are #{@all_userid-[@all_userid.last]}"
        @rest_ids=@all_userid-[@all_userid.last]
        @pdf_payment_result=AddPayment.find(@all_userid.last)
        @rest_ids.each do |ids|
            @count=0
            @pdf_vendor_details = PaymentVendor.find(ids)
        end
        puts "pdf vendor details #{@pdf_vendor_details}"
        @pdf_address=Vendor.where(:v_name => @pdf_vendor_details.v_name , :s_catagory => @pdf_vendor_details.v_catagory ).pluck(:v_address)
        @pdf_name=" #{Time.now.strftime("%d%m%Y%H%M%S") + '_' + @pdf_vendor_details.v_name + '_' + @pdf_vendor_details.v_catagory}.pdf " 
        pdf = render_to_string(pdf: "test.pdf", template: "payments/download_pdf.html.erb", encoding: "UTF-8", layout: 'application')
        send_data pdf ,:disposition => 'inline', filename: 'something.pdf', :type => 'application/pdf',:page_height => 'A2'
        save_path = Rails.root.join('pdfs', @pdf_name)
        File.open(save_path, 'wb') do |file|
            file << pdf
        end
    end
end
  

download_pdf.html.erb:

 <div class="modal-content">
            <div class="modal-header">

                <h4 class="modal-title" id="myModalLabel">Payment Advice</h4>
            </div>
            <div class="modal-body">
                <div class="bgcolorwhite">
                    <table class="table">
                      <tr>
                        <th style="width: 50%; text-align:left;">
                        <div class="pull-left"><%= wicked_pdf_image_tag 'odishalogo.png',:border => "0",:name => "odishagovtlogo",:style => "width:60px; height:60px;" %></div>
                        <div class="pull-left" style="padding:14px 0px 0px 10px;">Swargadwar, Puri Municipality <br />
                    Government of Odisha</div>
                        <div class="clearfix"></div> 
                        </th>
                        <th style="width:20%;"></th>
                        <th style="width: 30%; text-align:left; vertical-align: top;">Date : <%= Date.today.to_s  %><br />
                    Bill No.: <%= rand(10000) %> </th>       
                      </tr>
                      <tr>
                        <td colspan="3">
                            <strong>Vendor Name</strong> :<%= @pdf_vendor_details.v_name  %><br>
                            <strong>Address</strong> : <%= @pdf_address %>
                        </td>
                      </tr>
                      <tr>
                        <td colspan="3">
                            <table style="border: 1px solid #000000; width:100%;">
                                    <tr style="border: 1px solid #000000; background-color:#7878C3; color:#FFF;">
                                        <td style="width:10%; border-right: 1px solid #000000;">Sl. No</td>     
                                        <td style="width:20%; border-right: 1px solid black;">Receipt No</td>       
                                        <td style="width:50%; border-right: 1px solid black;">Item</td>
                                        <td style="width:20%; text-align:right; border-right: 1px solid black;">Amount</td>     
                                   </tr>
                                    <% @pdf_vendor_details.each do |details| %>
                                    <% @count=@count+1 %>
                                   <tr style="border: 1px solid black;">
                                        <td style="border-right: 1px solid black;"><%= @count %></td>
                                        <td style="border-right: 1px solid black;"><%= details.Receipt_No %></td>      
                                        <td style="border-right: 1px solid black;"><%= details.v_catagory %></td>
                                        <td style="text-align:right; border-right: 1px solid black;"><i class="fa fa-rupee"></i><%= details.v_amount  %></td>      
                                  </tr>
                                  <% end %>
                                   <tr>
                                        <td colspan="2" style="vertical-align: top;"><strong>Note :</strong>Thanks for your business<br />
                                        <% if @pdf_payment_result.p_catagory== 'Cash' %>
                                        <strong>Payment Mode :</strong><%= @pdf_payment_result.p_catagory %><br />
                                        <% end %>
                                        <% if @pdf_payment_result.p_catagory== 'Cheque'  %>
                                        <strong>Payment Mode :</strong><%= @pdf_payment_result.p_catagory %><br />
                                        <strong>Cheque No :</strong> <%= @pdf_payment_result.chk_no %>, <%= @pdf_payment_result.chk_details %>
                                        <% end %>
                                        </td>
                                        <td  style="text-align: right;">Total :<br /> Paid :<br /> Outstanding :<br /> Due Payment :<br /> Gross Total : </td>      
                                        <td style="text-align: right;"><i class="fa fa-rupee"></i> <%= @pdf_payment_result.paid_amount.to_s.to_d + @pdf_payment_result.outstanding.to_s.to_d %><br /><i class="fa fa-rupee"></i><%= @pdf_payment_result.paid_amount %><br /><i class="fa fa-rupee"></i> <%= @pdf_payment_result.outstanding  %><br /><i class="fa fa-rupee"></i> 120<br /><i class="fa fa-rupee"></i> 920</td>        
                                  </tr>
                            </table>
                        </td>
                      </tr>
                      <tr>
                        <td style="text-align:center;">
                            <div style="height:70px;">&nbsp;</div><br />
                            <span style="border-top:1px solid #000;">Puri Municipality</span>
                        </td>
                        <td></td>
                        <td style="text-align:center;">
                            <div style="height:70px;">&nbsp;</div><br />
                            <span style="border-top:1px solid #000;">Paid By</span>
                        </td>
                      </tr>
                    </table>
                </div>
            </div>
        </div>
  

total_paymentdetails.html.erb:

<table class="table table-bordered">
        <colgroup>
            <col class="col-md-1 col-sm-1">
            <col class="col-md-3 col-sm-3">
            <col class="col-md-2 col-sm-2">
            <col class="col-md-2 col-sm-2">
            <col class="col-md-2 col-sm-2">
            <col class="col-md-2 col-sm-2">
        </colgroup>
        <thead>
            <tr>
            <th class="text-center">Sl. No</th>
            <th class="text-center">Date</th>
            <th class="text-center">Total Claim</th>
            <th class="text-center">Paid</th>
            <th class="text-center">Outstanding</th>
            <th class="text-center">Payment Advice</th>
            </tr>
        </thead>
        <tbody>
        <tr>
            <td class="text-center">1</td>
            <td class="text-center"><%= @add_payment.created_at %></td>
            <td class="text-center"><%= @add_payment.total_claim %></td>
            <td class="text-center"><i class="fa fa-rupee"></i><%= @add_payment.paid_amount %></td>
            <td class="text-center"><i class="fa fa-rupee"></i><%= @outstanding %></td>
            <td class="text-center">
                <div class="btn-group">
                     <%= link_to "Download Pdf",payments_path(:format => "pdf",all_id: @userid ),:target => "_blank",method: :put %>
                </div>
                <div class="btn-group">
                    <a href="javascript:void(0)" title="" class="btn btn-xs btn-danger">
                        <i class="fa fa-download"></i>
                    </a>
                </div>
            </td>
        </tr>
        </tbody>
        </table>

请检查download_pdf方法,此处@pdf_vendor_details变量应包含使用不同ID的记录数组。但不会发生这种情况。请帮我解决此错误。

1 个答案:

答案 0 :(得分:0)

试试这个:

<%= link_to "Download Pdf",payments_path(:format => "pdf",:all_id => @userid.to_s),:target => "_blank",method: :put %>