即使显示设置为仅显示一些 foo,我的应用程序也会导出所有 foo 对象的列表。据我所知,我正在根据用户正在查看的视图设置要导出的 foos 数组。我无法弄清楚为什么页面显示适当@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>
该控制器的 index 和 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
谢谢。