Downloading XML file instead of rendering using Builder

╄→尐↘猪︶ㄣ 提交于 2019-12-11 02:18:35

问题


I am currently using Builder to make a custom XML export. However when the user clicks the button I want the .xml to download instead of rendering.

I have tried using send_data instead of render but that seems to be overridden by builder.

Also using ActiveAdmin (hence the member_action)

In my controller:

  member_action :show do
    @listing = Listing.find(params[:id])
    respond_to do |format|
      format.html
      format.xml 
    end
  end

Links to show.xml.builder

xml.instruct!
xml.XMLopener { |b|
  b.....
  ......
  ....
  }

I have looked at a few links below but I can't seem to get it working. Any ideas? Obviously with the current code it is just rendering, which works perfectly.

https://www6.software.ibm.com/developerworks/education/x-rubyonrailsxml/x-rubyonrailsxml-a4.pdf http://danengle.us/2009/05/generating-custom-xml-for-your-rails-app/


回答1:


Have you tried something like this

respond_to do |format|
  format.html
  format.xml { send_file :filename => 'mydoc.xml', :type=>"application/xml", :disposition => 'attachment' }
end


来源:https://stackoverflow.com/questions/18653336/downloading-xml-file-instead-of-rendering-using-builder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!