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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:06:28+00:00 2026-05-14T15:06:28+00:00

I have a model that has paperclip attachments. The model might be used in

  • 0

I have a model that has paperclip attachments.

The model might be used in multiple rails apps

I need to return a full (non-relative) url to the attachment as part of a JSON API being consumed elsewhere.

I’d like to abstract the paperclip aspect and have a simple virtual attribute like this:

  def thumbnail_url
    self.photo.url(:thumb)
  end

This however only gives me the relative path. Since it’s in the model I can’t use the URL helper methods, right?

What would be a good approach to prepending the application root url since I don’t have helper support? I would like to avoid hardcoding something or adding code to the controller method that assembles my JSON.

Thank you

  • 1 1 Answer
  • 3 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-14T15:06:29+00:00Added an answer on May 14, 2026 at 3:06 pm

    The url helper takes the host name from the incoming request. A model doesn’t need a request to exist, hence why you can’t use the URL helpers.

    One solution is to pass the request hostname to the thumbnail url helper.

    def thumbnail_url(hostname)
      self.photo.url(:thumb, :host => hostname)
    end
    

    Then call it from your controllers / views like this

    photo.thumbnail_url(request.host)
    

    It turns out that there isn’t really a foolproof way to get the host name of a server. If a user adds an entry to their /etc/hosts file, they can access the server with whatever host name they want. If you rely the incoming request’s hostname, it could be used to break the thumbnails.

    Because of this, I generally hardcode my site’s host name in an initializer. e.g. put this in config/initializers/hostname.rb

    HOSTNAME = 'whatever'
    

    — edit —

    It looke like in rails 3 you have to provide the :only_path => false parameter to get this to work. Here’s an example from the console:

    >> app.clients_path :host => 'google.com', :only_path => false
    => "http://google.com/clients"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model, Thing, that has a has_many with ThingPhoto, using Paperclip to
I have a model that has a ForeignKey to the built-in user model in
I have a model that has an arbitrary number of children entities. For simplicity
I have category model that has a tree structure. In my database I have
Specifically, I have a model that has a field like this pub_date = models.DateField(date
I have a Person model that has a foreign key relationship to Book ,
I have a parent model that has many child models, for example: App.Blog =
I have a User model that has the following default_scope: default_scope where(account_id: Account.current_account.id) If
I have a model that has a bone which has a translated offset from
I have a model that has a unique generic foreign key relationship: class Contact(models.Model):

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.