In this example I suppose to have a simple blog application where the user can download the content of a post in text format. So, in the posts controller we add the following action:
class PostsController < ApplicationController ... def download @post = Post.find(params[:id]) send_data @post.body, :filename => @post.title end ... endAfter this we have to add a new line into our config/routes.rb file to make this action accessible with a get request:
... resources :posts do member do get 'download' end end ...Now we can add a simple link in our view:
<%= link_to 'Download', download_post_path(@post), :title => 'Download post' %>Here we suppose to have the instance variable @post created somewhere (i.e. in the show action).
Now we can display our page and download the post as a text file.
Hope this helps, have fun with Ruby on Rails!