0

G'day guys, currently have almost finished writing a rails application that allows a CSV download of the database. This is generated when the index is first viewed.

Is there an easy way to insert a link to a helper that returns a CSV document? That is to say is it easy to insert links to helpers? This would make a lot of problems I've been having much easier

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Schroedinger
  • 1,273
  • 14
  • 32
  • You should accept some of your other answers and there are a couple of similar questions already on SO: http://stackoverflow.com/questions/1360682/rails-csvexport-to-csv-loop/1361155 – Toby Hede May 09 '10 at 11:31

2 Answers2

2

If you sticked to the general conventions, then you registered a mime-type for csv and return the csv file content via your #index action. So your link helper would be like this:

link_to 'export as csv', posts_path(:format => :csv)
Thomas R. Koll
  • 3,131
  • 1
  • 19
  • 26
0

If, in exchange, your file is generated WHEN index is first view but NOT BY Rails, you may want to avoid standart render and call send_data or send_file instead (check the api for them).

# in your controller:
def index

  # your suff here

  @csv_path = find_or_generate_csv_file
  send_data @csv_path, :type=>"text/csv", :disposition=>'attachment'
end

protected
  def find_or_generate_csv_file
    #your file generation logic
  end
Oinak
  • 1,805
  • 1
  • 12
  • 13