如何转换word文件中的 ruby文件,即(docx文件).对于pdf,我们大虾宝石.但是有没有word文件的宝石.我试图在word文件中转换我的html文件,以便它也可以为用户编辑.在那种情况下该怎么办?我打算
在initializers / mime_types.rb中:
Mime::Type.register 'application/vnd.ms-word', :msword
在你的控制器中:
说你要导出show动作:
def show @item = Item.find params[:id] respond_to do |format| format.html # show.html.erb format.xml { render :xml => @item } format.msword { set_header('msword', "#{@item.title}.doc") } format.pdf do render :pdf => 'Coming soon...', :layout => false end end end
在application_controller.rb中定义set_header:
def set_header(p_type, filename) case p_type when 'xls' headers['Content-Type'] = "application/vnd.ms-excel; charset=UTF-8'" headers['Content-Disposition'] = "attachment; filename=\"#{filename}\"" headers['Cache-Control'] = '' when 'msword' headers['Content-Type'] = "application/vnd.ms-word; charset=UTF-8" headers['Content-Disposition'] = "attachment; filename=\"#{filename}\"" headers['Cache-Control'] = '' end end
现在定义一个show.msword.erb#你可以使用任何模板处理程序,如haml等.
YOUR HTML HERE TO EXPORT TO DOC AS LIKE NORMAL ERB TEMPLATE