【问题标题】:Error trying to download using filnename format尝试使用 filnename 格式下载时出错
【发布时间】:2013-09-21 19:01:51
【问题描述】:

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

  • 我需要帮助自定义 = "日期" + "ejecutive selected" + ".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

这是我的控制器。 在这里,我输入了我正在尝试使用日期 + 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 的部分视图

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

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

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

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

【问题讨论】:

  • 看看report_by_ejecutive_xls的视图会很有帮助。

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2 ruby-on-rails-3.1


【解决方案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 方法了。

【讨论】:

  • 我试过你的代码,我得到了 nil:NilClass 的未定义方法 `name'
  • 你用什么网址下载xls文件?
  • //0.0.0.0:3000/policy_management/policy/generate_print_ejecutive_comercial?format=xls&search=3。 //0.0.0.0:3000/policy_management/policy/generate_print_ejecutive_comercial?method=get.
  • 哦,伙计们............你真的真的是这方面的主人...... :) 我真的真的很爱你哈哈......无话可说这个.. 非常感谢........@tithom 是 rails 的大师 :)
猜你喜欢
  • 1970-01-01
  • 2017-05-14
  • 1970-01-01
  • 1970-01-01
  • 2020-12-02
  • 2017-10-17
  • 2017-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多