0

我想定制当我保存为XLS格式进行下载:错误尝试使用filnename格式

  • 我需要帮助定制=“日期” +“ejecutive选择” +“的.xls”

但不工作

我的模型

class Policy < ActiveRecord::Base 
    unloadable 
    belongs_to :ejecutive 
    has_many :policy 

    def self.search(search) 
    if search 
     find(:all, :conditions => ["ejecutive_id = ? ", search.to_i ]) 
    else 
     find(:all) 
    end 
    end 
end 

class Ejecutive < ActiveRecord::Base 
    has_many :policies 
end 

这里是我的控制LER。 在这里,我把我的格式,我试图用日期+ ejecutive定制所选+ .xls的

class PolicyManagement::PolicyController < ApplicationController 
    def generate_print_ejecutive_comercial 
     @ejecutives = Ejecutive.find(:all) 
     @search = Policy.search(params[:search]) 
     @policies = @search.paginate(:page => 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 

这是我的看法

<% form_tag :controller=>"policy_management/policy",:action =>"generate_print_ejecutive_comercial", :method => 'get' do %> 
    <%= select_tag "search", options_for_select(@ejecutives.collect {|t| [t.name.to_s+" "+t.lastname1.to_s,t.id]}) %> 
    <%= submit_tag "Search", :name => nil %> 
<% end %> 

Results 
    <% @policies.each do |policy| %> 
    <p> <%= policy.num_policy%> </p> 
    <p> <%= policy.ejecutive.name %> </p> 
    <p> <%= policy.ejecutive.last_name %> </p> 
    <% end %> 
    <%= will_paginate @policies %> 

<%= link_to "Export", :controller=>"policy_management/policy",:action=>"generate_print_ejecutive_comercial" ,:format=>"xls",:search => params[:search],:page => params[:page] %> 

这是我的,我是出口到局部视图Excel中

************report_by_ejecutive_xls.************** 
<% @results.each do |policy| %> 
    <%= policy.num_policy%> 
     <% if !policy.ejecutive.blank? %> 
    <%= policy.ejecutive.name %><%= policy.ejecutive.lastname1 %><%= policy.ejecutive.lastname2 %> 
     <% end %> 
<% end %> 

我试过,但只保存第一ejecutive,我只希望在我看来

012 ejecutive选择
 format.xls { send_data render_to_string(:partial=>"report_by_ejecutive_xls"), :filename => "#{Date.today}#{@ejecutives.first.name}.xls" } 

请有人可以帮我解决这个问题吗?

+0

查看'report_by_ejecutive_xls'视图会有所帮助。 – spickermann

+0

好男人我编辑它 –

回答

1

控制器中不存在选定的执行对象。因此,需要重构一点得到在控制器选择ejecutive,然后用它得到政策

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方法。

+0

我试过你的代码,我得到了未定义的方法名称为零:NilClass –

+0

真的很奇怪是我所有的代码。 –

+0

你用什么网址下载'xls'文件? – tihom