我正在我的 Rails 应用程序中执行 AJAX 搜索。这是来自控制器的代码:
def show
@website = Website.find(params[:id])
if (current_user.id != @website.user_id)
redirect_to root_path
flash[:notice] = 'You are not owner!'
end
if params[:report] && params[:report][:start_date] && params[:report][:end_date]
@performance_reports = @website.performance_reports.where("created_at between ? and ?", params[:report][:start_date].to_date, params[:report][:end_date].to_date)
else
@performance_reports = @website.performance_reports
end
但是当我尝试生成 Excel 文档时,它总是在没有参数的情况下转到分支,因为 URL 中没有参数。
一位男士推荐我使用这篇文章。我试图实现它,但不能。
我不太了解这篇文章,我只是无法获取数据传递的位置(电子表格宝石)
这是代码:
def export
@website = Website.last
@data = @website.performance_reports
report = Spreadsheet::Workbook.new
spreadsheet = StringIO.new
contruct_body(spreadsheet, @data)
report.write spreadsheet
send_data spreadsheet.string, :filename => "yourfile.xls", :type => "application/vnd.ms-excel"
end
它给了我错误:
undefined method `contruct_body'
视图代码:
<%= form_tag( url_for, :method => :get, :id => "report") do%>
...show action posted above...
<% end %>
<%= link_to export_path do%>
<b>Export</b>
<% end %>
...working code without AJAX...
<%= link_to url_for(request.parameters.merge({:format => :xls})) do%>
<b>Export</b>
<% end %>
请告诉我我的错误在哪里或建议没有