Last active
February 18, 2017 12:21
-
-
Save pama/adff25ed1f4b796ce088ea362a08e1c5 to your computer and use it in GitHub Desktop.
Example downloading a csv file preserving ransack search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Users</h1> | |
<%= search_form_for @q, url: dashboard_index_path do |f| %> | |
<%= f.label :name_cont %> | |
<%= f.search_field :name_cont %> | |
<%= f.submit %> | |
<% end %> | |
<ul> | |
<% @users.each do |user| %> | |
<li><%= user.name %> [<%= user.devices.map {|device| device.name }.join(', ') %>]</li> | |
<% end %> | |
</ul> | |
<% 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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment