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 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 some problems trying to get a bag collection of a class to
I'm having some problems trying to perform a query. I have two tables, one
I have been trying to learn Erlang and have been running into some problems
I'm trying to build firefox but I'm having some problems. I currently have Visual
i have some problems with rowspan: var doc1 = new Document(); doc1.SetPageSize(PageSize.A4.Rotate()); string path
I am developing an app for iphone/ipad that I have some problems with. I
I'm trying to create a controller for a custom complex object but have some
I've been trying to deal with some delimited text files that have non standard
I have a problem trying to design some generic storage.. Basically I have the
I'm trying to get a regexp to solve the next problem: I have some

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.