Get absolute URL for paperclip attachment

前端 未结 7 2063
长发绾君心
长发绾君心 2021-02-02 10:19

Is it possible to get the absolute URI for a Paperclip attachment? Right now, the problem is that the production environment is deployed in a sub-URI (on Passenger: RackBa

相关标签:
7条回答
  • 2021-02-02 10:24
    asset_url(model.attachment_name.url(:style))
    

    Relevant github issue

    0 讨论(0)
  • 2021-02-02 10:24

    The most widely applicable way of doing this is to first define your asset hosts in the relevant config/environment file:

    config.action_controller.asset_host = "http://assethost.com"
    config.action_mailer.asset_host = "http://assethost.com"
    

    Then in views and mailers:

    asset_url(model.attachment.url(:style))
    

    In the console:

    helper.asset_url(model.attachment.url(:style))
    

    In a model:

    ApplicationController.helpers.asset_url(model.attachment.url(:style))
    
    0 讨论(0)
  • 2021-02-02 10:30

    You can do this:

    <%= image_tag "#{request.protocol}#{request.host_with_port}#{@model.attachment_name.url(:attachment_style)}" %>
    

    Or make a helper method to wrap it.

    def absolute_attachment_url(attachment_name, attachment_style = :original)
      "#{request.protocol}#{request.host_with_port}#{attachment_name.url(attachment_style)}"
    end
    

    And use it like this:

    <%= image_tag absolute_attachment_url(attachment_name, :attachment_style)}" %>
    

    Ex: Model = Person (@person), attachment_name = avatar, style = :thumb

    <%= image_tag absolute_attachment_url(@person.avatar, :thumb)}" %>
    
    0 讨论(0)
  • 2021-02-02 10:31

    According to this github issue, it is cleaner to use ActionController::Base.asset_host so it would result the helper:

      def add_host_prefix(url)
        URI.join(ActionController::Base.asset_host, url)
      end
    

    This supposes you have in every /config/environments/<environment>.rb file the following:

    Appname::Application.configure do
    
      # ....
    
      config.action_controller.asset_host = 'http://localhost:3000' # Locally
    
      # ....
    
    end
    
    0 讨论(0)
  • 2021-02-02 10:41

    You can add to your application.rb (or for specific enviroment in config/environments/*):

    config.paperclip_defaults = {
        url: "http://my.address.com/system/:class/:attachment/:id_partition/:style.:extension",
        path: ':rails_root/public/system/:class/:attachment/:id_partition/:style.:extension',
    }
    

    Restart and reimport your images.

    PS: obviously you can replace http://my.address.com with an environment variable.

    0 讨论(0)
  • 2021-02-02 10:42

    This doesn't solve the original poster's problem exactly (it operates in the view, not the model), but may be helpful for people who are trying to "get absolute URL for paperclip attachment" within their view: In the same way that

    image_tag(user.avatar.url(:large))
    

    puts the image itself into your view,

    image_url(user.avatar.url(:large))
    

    returns just the URL you'll need if you want to link to the asset directly (e.g. in a link_to call).

    0 讨论(0)
提交回复
热议问题