我想使用相同的方法制作PDF和Excel报告代码,并且只想传递参数一次,但是它显示错误
NoMethodError(未定义的方法`[]'对于nil:NilClass)。
控制器代码 -
def print_application_report
@from = params[:salary][:from_date]
@to = params[:salary][:to_date]
@company = params[:employee][:company_id]
@company_location = params[:employee][:company_location_id]
@department = params[:employee][:department_id]
@employees = Employee.where(company_id: @company.to_i,company_location_id: @company_location.to_i,department_id: @department).pluck(:id)
@travel_requests = TravelRequest.where(application_date: @from.to_date..@to.to_date,employee_id: @employees)
respond_to do |format|
format.js
format.xls {render template: 'travel_requests/application_datewise_report_xls.xls.erb'}
format.html
format.pdf do
render pdf: 'application_datewise_report_pdf',
layout: 'pdf.html',
orientation: 'Landscape',
template: 'travel_requests/application_datewise_report_pdf.pdf.erb',
# show_as_html: params[:debug].present?,
:page_height => 1000,
:dpi => '300',
:margin => {:top => 10, # default 10 (mm)
:bottom => 10,
:left => 20,
:right => 20},
:show_as_html => params[:debug].present?
end
end
end
答案 0 :(得分:0)
没有实际的错误,我在这里猜测。 你发送params为:
print_application_report_travel_requests_path(from_date: params[:salary][:from_date],to_date: params[:salary][:to_date], company_id: params[:employee][:company_id]....
此处参数from_date
的值为params[:salary][:from_date]
,但您直接发送参数,未包含在salary
密钥中。所以你的所有参数都可以直接在params
哈希中使用。
你可以尝试这样改变。
@from = params[:from_date]
@to = params[:to_date]
@company = params[:company_id]
@company_location = params[:company_location_id]
@department = params[:department_id]
答案 1 :(得分:0)
您可以使用三元运算符检查控制器中的状况,并检查是否存在params[:salary]
和params[:employee]
,
<强>控制器:强>
def print_application_report
@from = params[:salary] ? params[:salary][:from_date] : params[:from_date]
@to = params[:salary] ? params[:salary][:to_date] : params[:to_date]
@company = params[:employee] ? params[:employee][:company_id] : params[:company_id]
@company_location = params[:employee] ? params[:employee][:company_location_id] : params[:company_location_id]
@department = params[:employee] ? params[:employee][:department_id] : params[:department_id]
@employees = Employee.where(company_id: @company.to_i,company_location_id: @company_location.to_i,department_id: @department).pluck(:id)
@travel_requests = TravelRequest.where(application_date: @from.to_date..@to.to_date,employee_id: @employees)
respond_to do |format|
format.js
format.xls {render template: 'travel_requests/application_datewise_report_xls.xls.erb'}
format.html
format.pdf do
render pdf: 'application_datewise_report_pdf',
layout: 'pdf.html',
orientation: 'Landscape',
template: 'travel_requests/application_datewise_report_pdf.pdf.erb',
# show_as_html: params[:debug].present?,
:page_height => 1000,
:dpi => '300',
:margin => {:top => 10, # default 10 (mm)
:bottom => 10,
:left => 20,
:right => 20},
:show_as_html => params[:debug].present?
end
end
end
<强>解释强>
我所做的改变是,
<强> @from = params[:salary]
强>
要,
<强> @from = params[:salary] ? params[:salary][:from_date] : params[:from_date]
强>
params[:salary] ? params[:salary][:from_date] : params[:from_date]
的含义是,
如果有params [:salary],那么请params[:salary][:from_date]
,如果params[:salary]
不存在,那么请params[:from_date]
这一行也可以写成,
if params[:salary]
@from = params[:salary][:from_date]
else
@from = params[:from_date]
end