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 8200999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:39:03+00:00 2026-06-07T06:39:03+00:00

Here I have a piece of code that should work based on what I

  • 0

Here I have a piece of code that should work based on what I know of string interpolation in Ruby. Its inside a model class: “s3_file”

Basically wahat I am trying to accomplish is while saving a file to AWS S3, I would like to save them under a folder thats created at runtime using the following string interpolations. I am using Devise and cancan as authorization and authentication gems

the code below works:

 Paperclip.interpolates :prefix  do |attachment, style|
    "#{Date.today.to_s }/system"
 end

 has_attached_file(  :upload,
                  :path => ":prefix/:basename.:extension",
                  :storage => :s3,
                  :s3_credentials => {:access_key_id => "XXX",
                                      :secret_access_key => "XXX"},
                  :bucket => "XXX"
                )

But, I am trying to get the usersemail and insert that into the Paperclip block. This code doesn’t work as desired. The result of this code is not an exception but @curr_user_email is always null and as a result the folder on AWS S3 has no name. But the method does create a folder. How can I correct this?

THis code still does not work

if(@curr_user_signed_in)
  @aPrefix = "#{Date.today.to_s }/#{@curr_user_email}"
else
  @aPrefix = "#{Date.today.to_s }/system"
end

Paperclip.interpolates :prefix  do |attachment, style|
   @aPrefix
end


has_attached_file(  :upload,
                  :path => ":prefix/:basename.:extension",
                  :storage => :s3,
                  :s3_credentials => {:access_key_id => "xxx",
                                      :secret_access_key => "xxx"},
                  :bucket => "xxx"
                )

In my controller I have this bit of code:

  def index
   @s3_files = S3File.all

   @curr_user_signed_in = false
   if(user_signed_in?)
     @curr_user_signed_in = true
     @curr_user_email = current_user.email
   end
   respond_to do |format|
     format.html # index.html.erb
     format.json { render json: @s3_files }
   end
  end

So the real problem is that these
@curr_user_signed_in = true
@curr_user_email = current_user.email
are being set and are not null, but for some reason they cannot be read into the paperclip block

  • 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-07T06:39:06+00:00Added an answer on June 7, 2026 at 6:39 am

    Looks like you need to use paperclip interpolates. Credit really goes to this fellow:

    https://stackoverflow.com/a/852768/931209

    And here’s a snippet I believe should solve your problem.

        # interpolate in paperclip
        Paperclip.interpolates :maybe_user  do |attachment, style|
          @prefix =  "#{Date.today.to_s }/system/"
          if(user_signed_in?)
          {
            @prefix = "#{Date.today.to_s }/#{current_user.id}/"
          }
          @prefix
        end 
    

    then…

         has_attached_file(:upload,
                  :path => ":maybe_user/:basename.:extension",
                  :storage => :s3,
                  :s3_credentials => {:access_key_id => "XXXXX",
                                      :secret_access_key => "XXX"},
                  :bucket => "XXX"
                )
    

    Here’s the doc on the paperclip website:

    https://github.com/thoughtbot/paperclip/wiki/interpolations

    Hope that helps!

    Edit: If that gives you any shit, just add a .to_s to the strings and I think you should be alright. Though, I don’t know why it would.

    Update — 7/7 — Content Starts Below

    Preface: I have not used devise.

    The current_user method from devise cannot be accessed from within the model. It’s only available in the controllers.

    https://github.com/plataformatec/devise/blob/master/lib/devise/controllers/helpers.rb#L33

    You’ll need to add an attr_accessor to the model (S3File?) to current user, and then reference that instance variable within the model.

    Controller:

    class S3FileController < ApplicationController
    
        def create
            s = S3File.new
    
            if user_signed_in?
                s.current_user = current_user
            else
                s.current_user = nil
            end
    
            # Alternatively written
            # user_signed_in? ? s.current_user = current_user : s.current_user = nil
    
            s.upload = params[:file]
            s.save
    
        end
    
    end
    

    Model:

    class S3File < ActiveRecord::Base
    
        attr_accessor :current_user
    
        Paperclip.interpolates :maybe_user  do |attachment, style|
          @prefix =  "#{Date.today.to_s }/system/"
          if(@current_user)
          {
            @prefix = "#{Date.today.to_s }/#{current_user.id}/"
          }
          @prefix
        end 
    
    end
    

    I believe this should work as paperclip isn’t processing the file before S3File is saved. You then have access to the User object via @current_user so you can do something like this for the interpolation:

    Paperclip.interpolates :prefix  do |attachment, style|
       @pre = "#{Date.today.to_s}/system"
       if(@current_user)
         @pre = "#{Date.today.to_s}/#{@current_user.email}"
        end
        @pre
    end
    

    A couple of things to note:

    1.) The magic is really from the attr_accessor :current_user which allows you to store the value of the current_user object to the S3File. You can add as many other attr_accessors as you’d like if you want to store more information.

    2.) Not that there may not be a use case for it, but generally speaking, you don’t want to access methods available to your controllers from within the model… It sort of breaks the principles of MVC, as authentication should be done in the controllers, not in the model. I say this from my own experience having tried to do this long ago with authlogic and my (failed) results. YMMV, it’s just food for thought.

    3.) I’m pretty sure you have to do all the interpolation inside the paperclip block. There isn’t much of a reason not to either.

    [addendum]

    4.) Instance variables set in the controller are not available to the models. They are scoped to the class they’re created in… which is why I used attr_accessor =)

    [/addendum]

    Hopefully this gets you a few steps closer! Good luck.

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

Sidebar

Related Questions

I have a piece of code here that does not work despite me using
Here is the piece of code that I have used for Java 5.0 TreeSet<Integer>
my first question here! I have a problem with a piece of code that
I have a XAML code that should load my UserControl inside the TabControl .
I have a piece of code that I think should compile, but it doesn't.
Here I have piece of code, showing example of how I want to update
Well, I'm gonna be pretty straightforward here, I just have a piece of code
I have this piece of Open MP code here which performs an integeration of
Here a small piece of code for testing and explain the problem. I have
Here's my problem, I have a piece of jquery that works fine in modern

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.