开发者

Rails: Easiest way to provide file downloads?

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 开发者_StackOverflow社区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


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)


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜