Howto : add buttons to download data as xml, json or csv

giniedp edited this page Jul 4, 2012 · 5 revisions

First add buttons in the view

- content_for :fancygrid_users_buttons do
  %li= link_to "XML", "#", :onclick => "'xml'); return false;"
  %li= link_to "JSON", "#", :onclick => "'json'); return false;"
  %li= link_to "CSV", "#", :onclick => "'csv'); return false;"
= fancygrid :users

Next, in the controller you have to modify the fancygrid setup to not to paginate the data on the xml, json or csv requests, and add format renderers.

def index
  grid = fancygrid_for :users do |g|
    g.attributes :email, :first_name, :last_name
    g.ajax_url = users_path
    # paginate only on html request
    g.paginate = request.format.html?

  respond_to do |format|
    format.html { render }
    format.json { render :json => projects_grid.dump_records }
    format.xml { render :xml => projects_grid.dump_records }
    format.csv { # TODO: add code to convert records to csv }