尝试使用filnename格式下载时出错
当我保存到xls时,我正在尝试自定义格式:
- 我想帮助定制=“日期”+“执行选定”+“。xls”
但是没有用
我的模特
class Policy ["ejecutive_id = ? ", search.to_i ] ) else find(:all) end end end class Ejecutive < ActiveRecord::Base has_many :policies end
这是我的控制器。 在这里,我把我的格式设置为我正在尝试使用date + ejecutive选择+ .xls进行自定义
class PolicyManagement::PolicyController params[:page], :per_page =>10) @results= Policy.search(params[:search]) respond_to do |format| format.html format.xls { send_data render_to_string(:partial=>"report_by_ejecutive_xls"), :filename => "#{Date.today}#{@ejecutives.name}.xls" } end end
这是我的看法
"policy_management/policy",:action =>"generate_print_ejecutive_comercial", :method => 'get' do %> nil %> Results
"policy_management/policy",:action=>"generate_print_ejecutive_comercial" ,:format=>"xls",:search => params[:search],:page => params[:page] %>
这是我的部分观点,我正在出口到excel
************report_by_ejecutive_xls.**************
我尝试了这个,但只是保存了第一个导弹,我只想在我的视图中选择了导弹
format.xls { send_data render_to_string(:partial=>"report_by_ejecutive_xls"), :filename => "#{Date.today}#{@ejecutives.first.name}.xls" }
请有人帮我解决这个问题吗?
所选的主体对象在控制器中不可用。 因此需要重构一下以在控制器中获取所选的执行,然后使用它来获取策略
class PolicyManagement::PolicyController < ApplicationController def generate_print_ejecutive_comercial @ejecutives = Ejecutive.find(:all) #changed to find selected ejecutive here @selected_ejecutive = Ejecutive.find_by_id(params[:search]) if params[:search] # changed the search method for policies @search = @selected_ejecutive.try(:policies) || Policies.scoped @policies = @search.paginate( :page => params[:page], :per_page =>10) # changed this line as results is same as @search @results = @search @ejecutive_name = @selected_ejecutive.try(:name) || "All" respond_to do |format| format.html #changed the name here format.xls { send_data render_to_string(:partial=>"report_by_ejecutive_xls"), :filename => "#{Date.today}#{@ejecutive_name}.xls" } end end
现在不需要search
方法。