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