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

  • Home
  • SEARCH
  • 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 3311100
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:50:14+00:00 2026-05-17T21:50:14+00:00

I have some problems trying to custom the :path and :url options for has_attached_file

  • 0

I have some problems trying to custom the :path and :url options for has_attached_file with paperclip:

I have a polymorphic class named “Asset” that have :

class Asset < ActiveRecord::Base

  belongs_to :file_owner, :polymorphic => true

  has_attached_file :picture, :styles => { ...},
             :url => "/attachments/user_:user/dressing_:dressing/garment_:garment/category_:category/:basename_:style.:extension",  
             :path => ":rails_root/public/attachments/user_:user/dressing_:dressing/garment_:garment/category_:category/:basename_:style.:extension"  
end

The interpolations works well but I want to custom the path and the url depending on the file_owner_type

for instance, if I want the user’s picture path, I would like to just have

:path => ":rails_root/public/attachments/user_:user/:basename_:style.:extension

Thanks for your help

edit :
I think I did not explain myself correctly. I have already the interpolations that are created and works well.

I have an asset model that is polymorphic, the owner can be a user (for is avatar), a garment or a dressing.
And I want to have a different path depending on the file owner.
At this time, when I want to add a garment asset it works well the picture is put in

"/attachments/user_x/dressing_y/garment_z/category_u/something_style.jpg"

but if I just want a user picture this path will put the avatar in

"/attachments/user_x/dressing_/garment_/category_/something_style.jpg"

whereas I want to put it in

"/attachments/user_x/something_style.jpg".

thanks

  • 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-05-17T21:50:14+00:00Added an answer on May 17, 2026 at 9:50 pm

    Something like this in the url:

    :url => "/attachments/:path/:basename_:style.:extension",  
    

    then in the interpolations:

    Paperclip.interpolates :path do |attachment, style|
      if attachment.instance.file_owner_type == User.class.name
        # first set the _user variable (something like self.owner.id.to_s) 
        return "user_" + _user
      else
        # first set the _user, _dressing, _garmet, _category variables from your models
        return "user_#{_user}/dressing_#{_dressing}/garment_#{_garmet}/category_#{_category}/"
      end
    end
    

    Notice that you need to set the _user, _dressing, _garmet, _category variables from your models.

    Hope this helps.

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

Sidebar

Related Questions

I have been having some problems trying to get my PHP running. When I
I'm trying to build my first generic list and have run into some problems.
I have just started learning Erlang and am trying out some Project Euler problems
I discovered to have some problem to fully understand callbacks scoping when trying to
I have some problems with Miktex installed on Windows Vista Business SP1/32 bit. I
I have some problems on a site with the concurrent access to a list.
I am very new to C and I have some problems learning about pointers.
I have only installed Ruby1.9 on my machine. Have some kind of problems with
Looking for some direction here as I'm running into some migration problems. We have
I have run into a very interesting problem trying to debug my custom PHP

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.