Get absolute URL for paperclip attachment

前端 未结 7 2085
长发绾君心
长发绾君心 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: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)}" %>
    

提交回复
热议问题