Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8943059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:40:51+00:00 2026-06-15T11:40:51+00:00

I’m using paperclip to upload files to my server. If I don’t specify the

  • 0

I’m using paperclip to upload files to my server.
If I don’t specify the path, paperclip saves the file to a public folder, and then I can download it by accessing <%= @user.file.url %> in the view.
But if I specify the path to a non-public folder, it’s not possible getting the file from the view, obviously.

I would like to know some way to download the saved files in a private folder, from the web and from a ruby script.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:40:52+00:00Added an answer on June 15, 2026 at 11:40 am

    The first thing we need to do is add a route to routes.rb for accessing the files.

    Edit routes.rb and add the :member parameter in bold:

    resources :users, :member => { :avatars => :get }

    Now to get the avatar for user 7, for example, we can issue a URL like this:

     localhost:3000/users/7/avatars
    

    … and the request will be routed to the avatars action in the users controller (plural since a user might have more than one style of avatar).

    So now let’s go right ahead and implement the avatars method and add some code to download a file to the client. The way to do that is to use ActionController::Streaming::send_file. It’s simple enough; we just need to pass the file’s path to send_file as well as the MIME content type which the client uses as a clue for deciding how to display the file, and that’s it! Let’s hard code these values for the better understanding (update the path here for your machine):

        class UsersController < ApplicationController
          def avatars
           send_file '/path/to/non-public/system/avatars/7/original/mickey-mouse.jpg',
           :type => 'image/jpeg'
          end
        end
    

    Now if you type localhost:3000/users/7/avatars into your browser you should see the mickey image.

    Instead of hard coding the path in the avatars method, we obviously need to be able to handle requests for any avatar file attachment for any user record. To do this, configure Paperclip and tell it where the files are now stored on the file system, and which URL we have configured our routes.rb file to use.

    To do this, we need to add a couple of parameters to our call to has_attached_file in our User model (user.rb),

        has_attached_file :avatar,
        :styles => { :thumb => "75x75>", :small => "150x150>" },
        :path => 
        ':rails_root/non-public/system/:attachment/:id/:style/:basename.:extension',
        :url => '/:class/:id/:attachment' 
    

    But now we can generalize our code in UserController to handle any user, like this:

        def avatars
          user = User.find(params[:id])
          send_file user.avatar.path, :type => user.avatar_content_type
        end         
    

    Now we can test localhost:3000/users/7/avatars again to be sure that we haven’t broken anything.

    Cheers!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Paperclip to handle profile photo uploads in my app. They upload
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.