0

Привет всем, кто любит РЕЛЬСЫ, у меня есть проблема, я пытаюсь экспортировать с помощью «ejecutive_id» в формате XLS, но я не буду этого делать правильный код,Rails экспортирующие XLS, используя формат link_to

-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 [email protected]_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> 

Кажется, что мне нужно добавить некоторые Params к моему links_to. ... формат .... я не знаю, что добавить Кто-нибудь может проверить этот код?

ответ

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 [email protected]_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 
+0

Спасибо за ваш ответ, но я пользуюсь рельсами 2.3.5. Вот эта часть @list_policies_search = Policy.where (policy_options) не работает, где я могу сделать вместо «где» ???? –

+0

oh - вы можете настроить теги, так как они говорят, что рельсы 3. Во всяком случае, это та же концепция, но вместо этого ваш запрос будет '@list_policies_search = Policy.find (: all, policy_options)' – trh

+0

Wow ... now он говорит = «Неизвестный ключ (ы): удаленный« если я удаляю »policy_options = {: deleted => false}" показывает неправильное количество аргументов (2 для 1). Пожалуйста, будьте терпеливы со мной. Я не знаю много об этом. –