Skip to content

Instantly share code, notes, and snippets.

@neophiliac
Created July 7, 2011 17:05
Show Gist options
  • Save neophiliac/1069992 to your computer and use it in GitHub Desktop.
Save neophiliac/1069992 to your computer and use it in GitHub Desktop.
CSV link in Rails
View:
#block
%h3 Export
%ul{:class=>"horizontal"}
%li
- if controller.action_name == 'show'
= link_to "Open this Permit in Excel", permit_path(@item, :format => :csv)
- if controller.action_name == 'index'
= link_to "Open the list of Permits in Excel", permits_path(:format => :csv)
Model:
def to_csv
[
number,
name,
issued_date
].to_csv
end
Controller:
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @permits }
format.csv {
pcsv = @permits.collect { |p| "#{p.to_csv}" }.join
send_data pcsv, :type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment; filename=Permits-#{Time.now.strftime('%m-%d-%Y')}.csv" }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment