使用 Rails 传递两个查询字符串参数 3
Passing two query string parameter using Rails 3
我想在 link_to
标签中传递两个 id,这样通过使用两个 id,我可以从 DB.but 获取数据,在我的例子中,它显示一个 id 与 URL 一起出现在地址栏中因此出现以下错误。
Error:
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 不能 fetch.See 我的视图文件 我在 link_to
标签(即 <%= link_to "Download Pdf",download_pdf_path(:format => "pdf",:id1 => @add_payment.id,:id2 => @userid ),:target => "_blank" %>
)内传递两个 id。当download_pdf
操作正在执行我得到这个 URL 即 - http://localhost:3000/payments/download_pdf.pdf?id1=12
在其他 window 中我需要 id1 和 id2 这个 URL.Please 帮助我通过这些两个id作为查询字符串,这样就可以解决这个错误。
将以下内容添加到 routes.rb
get "payments/download_pdf/:id1/:id2" => "payments#download_pdf", :as => 'download_pdf'
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')
file_name = "#{Time.now.strftime("%d%m%Y%H%M%S")}.pdf"
send_data pdf ,:disposition => 'inline', filename: file_name, :type => 'application/pdf', :page_size => 'Letter'
save_path = Rails.root.join('pdfs', file_name)
File.open(save_path, 'wb') do |file|
file << pdf
end
end
这download_pdf 将创建一个唯一的时间戳
我想在 link_to
标签中传递两个 id,这样通过使用两个 id,我可以从 DB.but 获取数据,在我的例子中,它显示一个 id 与 URL 一起出现在地址栏中因此出现以下错误。
Error:
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 不能 fetch.See 我的视图文件 我在 link_to
标签(即 <%= link_to "Download Pdf",download_pdf_path(:format => "pdf",:id1 => @add_payment.id,:id2 => @userid ),:target => "_blank" %>
)内传递两个 id。当download_pdf
操作正在执行我得到这个 URL 即 - http://localhost:3000/payments/download_pdf.pdf?id1=12
在其他 window 中我需要 id1 和 id2 这个 URL.Please 帮助我通过这些两个id作为查询字符串,这样就可以解决这个错误。
将以下内容添加到 routes.rb
get "payments/download_pdf/:id1/:id2" => "payments#download_pdf", :as => 'download_pdf'
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')
file_name = "#{Time.now.strftime("%d%m%Y%H%M%S")}.pdf"
send_data pdf ,:disposition => 'inline', filename: file_name, :type => 'application/pdf', :page_size => 'Letter'
save_path = Rails.root.join('pdfs', file_name)
File.open(save_path, 'wb') do |file|
file << pdf
end
end
这download_pdf 将创建一个唯一的时间戳