Created
July 7, 2011 17:05
-
-
Save neophiliac/1069992 to your computer and use it in GitHub Desktop.
CSV link in Rails
This file contains 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
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