Rails使用link_to格式导出xls

时间:2013-09-16 16:45:24

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

大家好喜欢RAILS的人,我有一个问题我试图通过“ejecutive_id”导出到XLS,但我没有做正确的代码,

  -I created a select box where i can select all my ejecutives.
  -I created another select box where i can select by status_id.
  -I selected 1 ejecutive for example "Mogan"   (ejecutive_id = 1)
  -I selected 1 status form example "active"   (status_id = 0 )
  -After selecting both select boxes i click on "SEARCH"
  -After doing SEARCH i'm getting my results  (@list_policies this value in my code is before respond format)

这是我的控制器

 ****PROJECT/APP/CONTROLLER/policy_managment/policy.rb********

class PolicyManagement::PolicyController < ApplicationController

 def generate_print_ejecutive_comercial 

        @search_ejecutive = params[:search_ejecutive]
        @search_status = params[:status_id]

        @list_ejecutives_comision = Ejecutive.find(:all)
        @list_policies_search = Policy.deleted_is(0)

        if params[:search_ejecutive].to_i!=0
           @list_policies_search = @list_policies_search.ejecutive_id_is(@search_ejecutive)
        end

        if !params[:status_id].blank?
            if params[:status_id].to_i != 3
                  @list_policies_search = @list_policies_search.state_is(params[:status_id])
            end
        else
            @list_policies_search = @list_policies_search.state_is(0)
        end

        @status_id = params[:status_id]

        if !@search_dependent_dni.blank?
          if @list_dependents.blank?
            @list_dependents = Dependent.id_gt(0)
          end

          @list_dependents = @list_dependents.num_document_is(@search_dependent_dni)

          list_dependencies = []
          @list_dependents.each do |dependent|
                list_dependency_dependents = Dependency.find(:all, :conditions => { :dependent_id => dependent.id })
                list_dependency_dependents.each do |dependency|
                    list_dependencies << dependency
                end
          end

           policy_ids = []
            list_dependencies.each do |dependency|
                policy_ids << dependency.policy_id.to_i
            end

        end

        @list_policies_search = @list_policies_search.deleted_is(0)
        @list_policies = @list_policies_search.paginate(:page => params[:page], :per_page => 10) 
                @results = @list_policies_search.find(:all,:conditions=> { :ejecutive_id => @search_ejecutive }) 

        respond_to do |format|
            format.html
            format.xls
            format.js {
                render :update do |page|
                   page.replace_html 'table', :partial => 'table2'
                end
            }
        end
  end
 end 

这可能是问题,也是我可以导出的链接

     *********HERE IS MY VIEW***********
     <% form_remote_tag :url=>{:action=>"generate_print_ejecutive_comercial"},:before=>"load_close('loading_search')",:success=>"load_off('loading_search')" do -%> 
     <label>EJECUTIVES:</label>
     <%= select_tag 'search_ejecutive',"<option value=\"\">Select</option>"+options_for_select(@list_ejecutives_comision.collect {|t| [t.name.to_s+" "+t.lastname1.to_s,t.id]})%>
   </span>
        <label>STATUS :</label>
    <%= select_tag "status_id","<option value=\"3\">ALL</option>"+options_for_select([["active",0],["cancel",1],["no cancel ",2]],0) %>
  </span>

   <input name="Buscar" value="SEARCH" type="submit" /><span id="loading_search"></span>
   <% end %>         


    #HERE WITH THOSE LINKS I'M EXPORTING ONLY MY FIRST 10 VALUES 
   <%= link_to("Export Excel","http://localhost:3000/policy_management/policy/generate_print_ejecutive/generate_print_ejecutive_comercial.xls")%>
   <%= link_to "Export  XLS",:controller=>"policy_management/policy",:action=>"generate_print_ejecutive_comercial",:format=>"xls"  %>
   <%= link_to 'PRINT PDF', :controller=>"policy_management/policy",:action=>"generate_print_ejecutive_comercial", :format=>"pdf" %>

我正在导出我的部分视图,这取决于此

   @list_policies = @list_policies_search.paginate(:page => params[:page], :per_page => 10)

这是我的部分视图,我正在使用@list_policies

 ********************PARTIAL VIEW THAT I WANT TO EXPORT*********
 <table>

  <% @list_policies.each do |policy| %>
   <tr>
     <td><div class="nobreak"><%= policy.num_policy%></div></td>
     <td><div class="nobreak">
  <% if !policy.ejecutive.blank? %>
        <%= policy.ejecutive.name %><%= policy.ejecutive.lastname1 %><%= policy.ejecutive.lastname2 %>
  <% end %></div>
    </td> 
    <td><div class="nobreak"><%= policy.str_state%></div></td>
  </tr>
  </table>

似乎我需要在我的links_to ....格式中添加一些参数....我不知道要添加什么 有人可以查看这段代码吗?

1 个答案:

答案 0 :(得分:0)


似乎使用选项哈希来动态构建查询可能更容易/更清晰。我会这样做:

class PolicyManagement::PolicyController < ApplicationController

  def generate_print_ejecutive_comercial

    @list_ejecutives_comision = Ejecutive.find(:all)

    policy_options = {:deleted => false}
    policy_options = policy_options.merge({:ejecutive_id => params[:search_ejecutive]}) unless params[:search_ejecutive].blank?
    policy_options = policy_options.merge({:state => params[:status_id]}) unless params[:status_id].blank? and (params[:status_id] != 3)
    @list_policies_search = Policy.all(policy_options)

    if !@search_dependent_dni.blank?
      if @list_dependents.blank?
        @list_dependents = Dependent.id_gt(0)
      end

      @list_dependents = @list_dependents.num_document_is(@search_dependent_dni)

      list_dependencies = []
      @list_dependents.each do |dependent|
        list_dependency_dependents = Dependency.find(:all, :conditions => { :dependent_id => dependent.id })
        list_dependency_dependents.each do |dependency|
          list_dependencies << dependency
        end 
      end

      policy_ids = []
      list_dependencies.each do |dependency|
        policy_ids << dependency.policy_id.to_i
      end

    end

    @list_policies = @list_policies_search.paginate(:page => params[:page], :per_page => 10)

    respond_to do |format|
      format.html
      format.xls
      format.js {
        render :update do |page|
        page.replace_html 'table', :partial => 'table2'
        end 
      } 
    end

  end
end