Error trying to download using filnename format

2019-06-14 10:15发布

问题:

I'm trying to customize the format when i'm saving to xls :

  • I want help customizing= "Date" + "ejecutive selected" + ".xls"

But is not working

My models

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

Here is my controller. Here i put my format that i'm trying to customize with date + ejecutive selected + .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

Here is my view

<% 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] %>

This is my partial view that i'm exporting to 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 %> 

I tried this but is only saving the first ejecutive and i only want ejecutive selected in my view

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

Please somebody can help me with this problem?

回答1:

The selected ejecutive object is not available in the controller. So need to refactor a bit to get the selected ejecutive in controller and then use it get the policies

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

There is no need of the search method now.