views:

87

answers:

2

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

+2  A: 

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
A: 

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
Fer