使用Rails 3传递两个查询字符串参数

时间:2015-06-03 05:35:41

标签: ruby ruby-on-rails-3.2

我想在link_to标记中传递两个id,以便通过使用两个id我可以从DB获取数据。但在我的情况下,它显示一个ID在地址栏中带有URL,因此我得到了跟随错误。

  

错误:

ActiveRecord::RecordNotFound in PaymentsController#download_pdf

Couldn't find PaymentVendor without an ID
Rails.root: C:/Site/swargadwara_puri1

Application Trace | Framework Trace | Full Trace
app/controllers/payments_controller.rb:85:in `download_pdf'

我的代码文件已在下面解释。

  

total_payment_result.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",download_pdf_path(:format => "pdf",:id1 => @add_payment.id,:id2 => @userid ),:target => "_blank" %>
                </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>
  

payments_controller.rb:

class PaymentsController < ApplicationController
    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[:id]
            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 )
                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
                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] )
                    if @add_payment
                        @abc=true
                    end
                end
            end
        else
            if params[:add_payment][:p_catagory]=="Cheque"
                @chk=true
            else
                @Cash=true
            end
        end
    end
    def download_pdf
        @pdf_payment_result=AddPayment.find(params[:id1])
        @pdf_vendor_details=PaymentVendor.find(params[:id2])
        @pdf_address=Vendor.where(:v_name => @pdf_vendor_details.v_name , :s_catagory => @pdf_vendor_details.v_catagory )
        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'
        save_path = Rails.root.join('pdfs','payment.pdf')
        File.open(save_path, 'wb') do |file|
            file << pdf
        end
    end
end
  

route.rb:

SwargadwaraPuri::Application.routes.draw do
  root :to => "admins#index"
  post "session/login_admin" => "sessions#login_admin"
  get "homes/home" => "homes#home"
  get "admins/index" => "admins#index"
  get  "sessions/logout_admin" => "sessions#logout_admin"
  post "homes/scan_report" => "homes#scan_report"
  get "reports/view_report" => "reports#view_report"
  get "users/create_client" => "users#create_client"
  post "users/client_reg" => "users#client_reg"
  get "homes/user_home" => "homes#user_home"
  get "vendors/vendor_add" => "vendors#vendor_add"
  get "vendors/vendor_edit" => "vendors#vendor_edit"
  get "payments/payment" => "payments#payment"
  get "payments/view_payment" => "payments#view_payment"
  post "vendors/create_vendor" => "vendors#create_vendor"
  post "homes/payment" => "homes#payment"
  post "vendors/update_vendor" => "vendors#update_vendor"
  post "payments/check_type" => "payments#check_type"
   get "payments/check_type" => "payments#check_type"
   post "payments/check_dropdown" => "payments#check_dropdown"
   get "payments/paym" => "payments#paym"
   post "payments/add_payment" => "payments#add_payment"
   get "payments/download_pdf" => "payments#download_pdf", :as => 'download_pdf'
end
  

paymentdetails.html.erb:

<table class="table table-bordered">
        <colgroup>
            <col class="col-md-1 col-sm-1">
            <col class="col-md-1 col-sm-1">
            <col class="col-md-3 col-sm-3">
            <col class="col-md-3 col-sm-3">
            <col class="col-md-4 col-sm-4">
        </colgroup>
        <thead>
            <tr>
                <th class="text-center"><input type="checkbox"></th>
                <th class="text-center">Sl. No</th>
                <th class="text-center">Date</th>
                <th class="text-center">Receipt No.</th>
                <th class="text-center">Amount</th>
            </tr>
        </thead>
        <tbody>
            <% @result.each do |r| %>
            <%= hidden_field_tag 'infos[]',r.v_amount %>
            <tr>
                <th class="text-center"><%= check_box_tag "deatils[]" ,r.id,{} ,:id => r.id %></th>
                <td class="text-center"><%= r.id %></td>
                <td class="text-center"><%= r.c_date %></td>
                <td class="text-center"><%= r.Receipt_No %></td>
                <td class="text-center"><i class="fa fa-rupee"></i><%= r.v_amount %></td>
            </tr>
            <% end %>
     </tbody>
</table>

实际上download_pdf方法中出现错误,因为id2无法获取。查看我的视图文件我在link_to标记内传递了两个id(即<%= link_to "Download Pdf",download_pdf_path(:format => "pdf",:id1 => @add_payment.id,:id2 => @userid ),:target => "_blank" %>)。当{{1动作正在执行我正在获取此URL即 - download_pdf在其他窗口中,我需要id1和id2与此URL。请帮助我将这两个id作为查询字符串传递,以便可以解决此错误。< / p>

1 个答案:

答案 0 :(得分:1)

将以下内容添加到routes.rb

WHERE ....
AND ND.Title = CASE WHEN @param1 = 'blank' 
                    THEN "Return all including null" 
                    ELSE "Return" @param1

此download_pdf将创建一个唯一的时间戳