2013-06-05 38 views
1

即使顯示設置爲只顯示一些foos,我的應用程序也會導出所有foo對象的列表。就我所知,我正在根據用戶正在查看的視圖來設置要導出的foos數組。我無法弄清楚爲什麼頁面顯示的是合適的@foos,但沒有輸出合適的@foos_for_xls如何讓rails excel導出到過濾索引上工作,當前導出整個對象列表

我得從我的指數這一呼籲:

<%= link_to "Export to Excel", foos_path(format: "xls"), :class => "btn btn-primary pull-right" %> 

,除非我名單上的過濾器,它工作正常通過在PARAM通過從下拉導航欄按鈕,如:

<li><%= link_to "SPACE" , :action => :index, :location_id => 2 %></li> 

那控制器的索引和respond_to看起來像:

def index 
     @foos = Foo.order(:name) 

     if params[:location_id]  
     @foos = @foos.order(:name).by_location(params[:location_id]).search(params[:search]) 
     @foos_for_xls_loc = @cabinets 
     else 
     @foos_for_xls = @foos 
     end 

    @foos = @foos.page params[:page] 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @foos } 
     if params[:location_code] 
     format.csv { send_data @foos_for_xls_loc.to_csv } 
     format.xls { send_data @foos_for_xls_loc.to_csv(col_sep: "\t") } 
     else 
     format.csv { send_data @foos_for_xls.to_csv } 
     format.xls { send_data @foos_for_xls.to_csv(col_sep: "\t") } 
     end 
    end 
    end 

這裏是模型的方法, ering:

def self.by_loc(location_id) 
    if location_id 
     joins(:location).where("location.id = ?", location_id) 
    else 
     scoped 
    end  

謝謝。

回答