我应该在哪里放置生成 Excel 电子表格的代码?
我正在使用 spreadsheet gem 生成本机 Excel 文件。这不是 CSV、XML 文件。使用普通的 Ruby 代码来创建该文件。生成的 Excel 文件(保存在 StringIO 中)使用 send_data
方法转发到客户端。我需要 send_data
方法,因为它的参数如 disposition
。
Excel 的数据是在控制器方法中检索的,就像普通的 HTML、JS 请求一样。不过,我将生成电子表格的代码放置在控制器受保护的方法中。不是我应该看到的。
是否有符合 MVC 设计模式的优雅解决方案来解决上述问题?
更新:对于上述问题,没有流行且被所有人接受的解决方案,但至少我知道所有可能的想法。
I am using spreadsheet gem to generate native Excel file. This is not CSV, XML file. Ordinary Ruby code is used to create the file. The generated Excel file (kept in StringIO
) is forwarded to a client using send_data
method. I need send_data
method because of its parameters like disposition
.
The data for the Excel is retrieved in controller method just like for ordinary HTML, JS requests. However I placed the code to generate the spreadsheet in controller protected method. Not in a view as I should.
Is there an elegant solution to above problem compliant with MVC design pattern?
Update: There is no popular and accepted by all solution to above problem but at least I know all possible ideas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
lib 目录是存放严格意义上不属于 MVC 结构一部分的代码的地方,但多个模型、视图或控制器都需要这些代码。需要时可以通过
require
引入它。但是,如果您只需要单个控制器中的代码,那么您最好将其放入该控制器的帮助程序中。这样它就会自动加载并触手可及。另外,它是有道理的:它是帮助特定控制器的代码。
无论哪种方式,都不要将其留在控制器中或尝试将其插入您的视图中。
The lib directory is meant as a place for code that isn't strictly part of the MVC structure, but will be needed by multiple models, views, or controllers. It can be brought in with a
require
when needed.However, if you only need the code in a single controller, you'd be just as well off putting it into that controller's helper. That way it's auto-loaded and at your fingertips. Plus, it makes sense: it's code to help a specific controller.
Either way, don't leave it in your controller or try to wedge it into your view.
render
方法的方法。to_excel
方法的方法,该方法生成可提供给 xls 例程的哈希值。这样做,你会得到一些真正“Railsy”的东西。在你的控制器中你只需调用
渲染:xls => @model
render
method.to_excel
method which generates a hash that you can provide to your xls routine.Doing it this way, you'll get something really "Railsy". In your controller you'll just call
render :xls => @model
我今天刚刚为我的应用程序做了这个,希望这对 excel o/p 有帮助;从未使用过任何插件
控制器:
默认导出
pr = Program.find(会话[:pr_id])
headers['Content-Type']="application/vnd.ms-excel"
headers['Content-Dispositon']='attachment;filename="report.xls"'
@voucherdatas = Voucherdata.find_all_by_pr_name(pr.pr_name)
最终
视图:export.html.erb
manager
"reports/voucherdatas", :object =>@voucherdatas %>
paths.rb
map.resources :reports ,:collection =>{:export =>:get}
无论您想要链接在哪里,都给出
link_to "Export As Excel", export_reports_url, :popup=>true
i just did this today for my app hope this helps for an excel o/p ; never used any plugin
controller:
def export
pr = Program.find(session[:pr_id])
headers['Content-Type']="application/vnd.ms-excel"
headers['Content-Dispositon']='attachment;filename="report.xls"'
@voucherdatas = Voucherdata.find_all_by_pr_name(pr.pr_name)
end
view: export.html.erb
manager
"reports/voucherdatas", :object =>@voucherdatas %>
routes.rb
map.resources :reports ,:collection =>{:export =>:get}
whereever u want the link give
link_to "Export As Excel", export_reports_url, :popup=>true