尝试使用文件名格式下载时出错 [英] Error trying to download using filnename format

查看:53
本文介绍了尝试使用文件名格式下载时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在保存到 xls 时自定义格式:

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

  • 我需要帮助定制=日期"+已选择的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

这是我的控制器.在这里,我放置了我尝试使用日期 + 弹出选择 + .xls 自定义的格式

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

这是我的观点

<% 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的部分观点

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

我试过了,但只保存了第一个 ejecutive,我只想在我的视图中选择 ejecutive

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?

推荐答案

所选的弹出对象在控制器中不可用.所以需要稍微重构一下以在控制器中获取选定的 ejecutive,然后使用它获取策略

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

现在不需要 search 方法了.

There is no need of the search method now.

这篇关于尝试使用文件名格式下载时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆