自定义文件名rails rails问题

时间:2013-09-21 17:43:36

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

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

  • 我想要帮助定制=“日期”+“执行选定”+“。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

这是我的控制器。 在这里,我把我的格式设置为我正在尝试使用date + 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"), :filename => Date + @ejecutives.name"reporte.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] %>

我试过这个

      respond_to do |format|
        format.html
        format.xls { send_data render_to_string(:partial=>"report_by_ejecutive"), :filename => Date + @ejecutive.name + ".xls" }
      end  

1 个答案:

答案 0 :(得分:1)

+课程或Date之类的特定日期不能Date.today。但以下工作。

:filename => "#{Date.today}#{@ejecutive.name}.xls"

"#{something}"评估something,对结果调用to_s并将其插入字符串。