0

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

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

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"), :filename => Date + @ejecutives.name"reporte.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] %>

I tried this

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

1 Answer 1

1

You cannot + something to the Date class or a specific date like Date.today. But the following works.

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

"#{something}" evaluates something, calls to_s on the result and inserts its into the string.

Sign up to request clarification or add additional context in comments.

5 Comments

i used your code and got undefined method `name' for nil:NilClass
i change it for @ejecutives.name and i got undefined method `name' for #<Array:0x7f9286ddb560>
Right. You have only @ejecutives = Ejecutive.find(:all) and not one specific Ejecutive. Try @ejecutives.first.name
ok man is working but is only saving my first ejecutive. Is there any other way to save by ejecutive selected??
Perhaps one download link for each? But that is a different problem. Please clean up your code with the current learnings and start a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.