First you want to set up a model for storing tokens:
rails g model DownloadToken token:string expires_at:timestamp
download_token.rb
class DownloadToken < ActiveRecord::Base attr_accessible :token, :expires_at before_create :generate_token def generate_token self.token = SecureRandom.base64(15).tr('+/=lIO0', 'abc123') end end
Then configure the controller to process the submitted form (or make changes to the existing action) and create a token, send an email, etc.
class FooController < ApplicationController def create
You want to make sure your postal view has something like:
<%= link_to "Click Me", "/files/downloads?token=#{@token.token}" %>
You will also want to configure the controller responsible for the boot, and it should look something like this:
class FileController < ApplicationController before_filter :check_token def check_token redirect_to :back, :flash => {:error => "Bad link"} if DownloadToken.where("token = ? and expires_at > ?", params[:token], Time.now).nil? end def download send_file '/home/your_app/downloads/yourfile.zip', :type=>"application/zip", :x_sendfile=>true end end
routes.rb (if Foo is already configured as a RESTful resource)
match 'files/download' => 'files#download'
This code has not been verified, but it should cover most of what you need and give you an idea of ββwhich direction you want to take.
Additional Information:
source share