title: secure upload/download file with carrierwave gem rails4
body: <p>Authenticate user can download and upload file , this is the main purpose of my project.I want to secure my file downloads, so that only authenticate user can download file. For this, I use <code>gem carrierwave</code>  and carrierwave/wiki <a href="http://github.com/carrierwaveuploader/carrierwave/wiki/How-To%3A-Secure-Upload" rel="nofollow">"How To: Secure Upload"</a> . But when I click my download url , its says "HTTP/1.1 500 Internal Server Error"</p>
  
  <p>Here is addfiles_controller.rb file:</p>
  
  <pre><code>class AddfilesController &lt; ApplicationController
    before_action :logged_in
  
    def index
      @addfiles = Addfile.all
    end
  
    def new
      @addfile = Addfile.new
    end
  
    def create
      if admin_signed_in?
        @addfile = current_admin.addfiles.build(addfile_params)
      else
        @addfile = current_user.addfiles.build(addfile_params)
      end
  
  
      if @addfile.save
        redirect_to addfiles_path
      else
        render "new"
      end
    end
  
    def destroy
      @addfile = Addfile.find(params[:id])
      @addfile.destroy
      redirect_to addfiles_path
    end
  
  
    def download
      path = "/#{addfile.addfile}"
      send_file path, :x_sendfile=&gt;true
    end
  
  
  
    private
    def addfile_params
      params.require(:addfile).permit(:name, :attachment)
    end
  end
  </code></pre>
  
  <p>config/initializers/carrierwave.rb file:</p>
  
  <pre><code>CarrierWave.configure do |config|
    # These permissions will make dir and files available only to the user running
    # the servers
    config.permissions = 0600
    config.directory_permissions = 0700
    config.storage = :file
    # This avoids uploaded files from saving to public/ and so
    # they will not be available for public (non-authenticated) downloading
    config.root = Rails.root
  end
  </code></pre>
  
  <p>routes.rb file:</p>
  
  <pre><code>FileDownload::Application.routes.draw do
  
    match "/uploads/:id/:basename.:extension", :controller =&gt; "addfiles", :action =&gt; "download", via: :get
  
    resources :addfiles do
      collection  do
        get 'all_users'
      end
    end
    root "addfiles#index"
    devise_for :admins
    devise_for :users
  
  end
  </code></pre>
  
  <p>in my views:</p>
  
  <pre><code>&lt;%= link_to File.basename(file.attachment_url), "/uploads/#{file.id}/#{File.basename(file.attachment_url)}" %&gt;
  </code></pre>
  
  <p><strong>attachment_uploader.rb file</strong></p>
  
  <pre><code>class AttachmentUploader &lt; CarrierWave::Uploader::Base
  
    storage :file
  
    def store_dir
      "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    end
  
    def extension_white_list
      %w(pdf doc htm html docx)
    end
  
  end
  </code></pre>
  
  <p>Error Trace::</p>
  
  <pre><code>Started GET "/uploads/13/ARTICLE_FINAL_.pdf" for 127.0.0.1 at 2014-09-04 14:39:53 +0600
  Processing by AddfilesController#download as */*
    Parameters: {"id"=&gt;"13", "basename"=&gt;"ARTICLE_FINAL_", "extension"=&gt;"pdf"}
    ←[1m←[36mUser Load (0.0ms)←[0m  ←[1mSELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1←[0m
  Completed 500 Internal Server Error in 4ms
  
  NameError (undefined local variable or method `addfile' for #&lt;AddfilesController:0x46baa10&gt;):
    app/controllers/addfiles_controller.rb:37:in `download'
  
  
    Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.0ms)
    Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.4/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
    Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
    Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (47.0ms)
  [2014-09-04 14:39:53] ERROR Errno::ECONNRESET: An existing connection was forcibly closed by the remote host.
          C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:80:in `eof?'
          C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:80:in `run'
          C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
  </code></pre>
  
  <p>whats the problem here?? Please give me your suggestion. </p>