docs/docs/going-further/exporting-to-csv
sidebar_position: 2
title: CSV Export
Exporting to CSV
Example downloading a csv file preserving ransack search, based on this gist
“`jsx title=‘index.html.erb’
Users
<%= search_form_for @q, url: dashboard_index_path do |f| %>
<%= f.label :name_cont %>
<%= f.search_field :name_cont %>
<%= f.submit %>
<% end %>
-
<% @users.each do |user| %>
- <%= user.name %> [<%= user.devices.map {|device| device.name }.join(‘, ’) %>] <% end %>
<% if params[:q] %>
<%= link_to ‘Export 1’, dashboard_index_path({name: params[:q][:name_cont]}.merge({format: :csv})) %>
<% else %>
<%= link_to ‘Export 2’, dashboard_index_path(format: ‘csv’) %>
<% end %>
```jsx title='user.rb' require 'csv' class User < ApplicationRecord has_many :devices def self.get_csv(users) CSV.generate do |csv| csv << ["Name", "Devices"] users.each do |user| csv << [user.name, user.devices.map{|device| device.name}.join(', ')] end end end end