ディスプレイが一部の foo のみを表示するように設定されている場合でも、私のアプリはすべての foo オブジェクトのリストをエクスポートします。私の知る限りでは、ユーザーが見ているビューに基づいてエクスポートする foo の配列を設定しています。ページに適切なものが表示されているのに、適切なもの@foos
がエクスポートされていない理由がわかりません@foos_for_xls
。
インデックスから次の呼び出しがあります。
<%= link_to "Export to Excel", foos_path(format: "xls"), :class => "btn btn-primary pull-right" %>
次のようなドロップダウンナビゲーションバーボタンからパラメーターを渡してリストをフィルタリングしない限り、正常に機能します。
<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
フィルタリングを行うモデル メソッドは次のとおりです。
def self.by_loc(location_id)
if location_id
joins(:location).where("location.id = ?", location_id)
else
scoped
end
ありがとう。