Skip to content

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 => "Fancygrid.users.download('xml'); return false;"
  %li= link_to "JSON", "#", :onclick => "Fancygrid.users.download('json'); return false;"
  %li= link_to "CSV", "#", :onclick => "Fancygrid.users.download('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?
    g.find
  end

  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 }
  end
end
Clone this wiki locally