Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

image_path helper won't generate absolute paths without asset_host

#1015

The image_tag and image_path helpers won't generate absolute paths unless ActionController::Base.asset_host is set.

The more fundamental cause is the ActionView::Helpers::AssetTagHelper#compute_public_path method and arguably the ActionView::Helpers::AssetTagHelper#compute_asset_host which returns an empty string when no asset_host is set.

This inability to return absolute paths without an asset host is particularly painful when using ActionMailer because HTML emails practically require the use of absolute paths.

I propose modifying compute_asset_host to return an appropriate asset host even if the AC::Base.asset_host is not set (where possible).

When asset_host is not set, we will rely on the ability of the root_url method to return a reasonable value. In many cases, this relies on the presence of default_url_options -including the ActionMailer case.

Reported by Chris Hapgood · September 10th, 2008 @ 10:35 PM

State: stale
Milestone: 3.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Chris Hapgood
    Chris Hapgood

    Attached patch to solve problem. Tests coming next...

    September 10th, 2008 @ 10:42 PM

  2. Michael Koziarski
  3. Pratik
    Pratik
    • State changed from new to incomplete

    Hey Chris,

    Patch doesn't apply anymore.

    October 4th, 2008 @ 03:09 PM

  4. Chris Hapgood
    Chris Hapgood

    When I wrote this changeset up on the mailing list, nobody replied. No positive replies, no negative replies. So I'm led to believe that with ActionMailer people are either using an asset host or they have alternative workarounds to get linked images.

    In either case, the lack of interest has made me put my work into other issues for the time being. I'll try to get back to this ticket if you think it's worthwhile. Otherwise, let's close it. -Chris

    October 5th, 2008 @ 02:21 PM

  5. Pratik
    Pratik
    • State changed from incomplete to hold

    Let's keep it on 'hold' for now and tackle it post 2.2 ( or till someone else faces the problem).

    October 5th, 2008 @ 02:28 PM

  6. Jonathan del Strother
    Jonathan del Strother

    I for one would like to see this fixed. The attached patch is a little cleaner than the original and includes a test.

    Any thoughts on the use of root_url for computing this? Seems a little weird to be relying on something that may or may not be defined in routes, but I'm not sure there's a better way...

    January 15th, 2009 @ 03:20 PM

  7. Pratik
    Pratik
    • Milestone cleared.
    • State changed from hold to open

    January 15th, 2009 @ 03:30 PM

  8. Pratik
  9. Pratik
    Pratik

    Umm, why can't we use default_url_options[:host] ?

    January 18th, 2009 @ 04:05 AM

  10. Pratik
    Pratik
    • Milestone set to 2.x

    January 28th, 2009 @ 07:56 PM

  11. Marko
    Marko

    I'm building newsletter now and this would be very useful feature.

    January 29th, 2009 @ 07:34 PM

  12. Gregory Tomei
    Gregory Tomei

    +1 I would like to have absolute URLs possible in image_tag also

    February 11th, 2009 @ 07:11 PM

  13. Ryan Sandridge
    Ryan Sandridge

    Specifying default_url_options[:host] in ActionMailer does not result in absolute image tags (or I'm doing something wrong).

    October 21st, 2009 @ 06:36 PM

  14. gonzoyumo
    gonzoyumo

    Hi

    +1 too. Still stuck with html emails. would be intresting to have such a feature.

    what about it ?

    April 13th, 2010 @ 11:49 PM

  15. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  16. masone
    masone
    • Importance changed from to

    Regarding Jonathan del Strothers patch.

    • The patch does not apply to master, but it does apply to 2-3-stable.
    • Tests do pass on 2-3-stable

    I personally think that the current behavior is perfectly fine. There is no asset host defined, so there is no absolute asset path. The behaviour only becomes obvious when you use the helper in mailer templates. The same problem applies to url_for too by the way.

    I don't like the idea of using the root_url as a fallback. It just doesn't feel right to me. And I don't think that it's a good idea to rely the implementation on route helpers.

    There are a few things I would reconsider:

    • Is there really the need for a change? Just setting the asset host would do the trick.
    • Find a generic solution that also fixes the url_for helper, not just the asset tag.
    • Think about an other solution than using root_url.
    • Return host instead of nil since that would match the behavior on line 577 (host would be nil in that case anyway)

    July 16th, 2010 @ 04:45 PM

  17. Jarl Friis
    Jarl Friis

    I would like to see something like ActionView::Helpers::AssetTagHelper#image_url that returns the absolute url in stead of just the path as ActionView::Helpers::AssetTagHelper#image_path does.

    Jarl

    August 27th, 2010 @ 03:48 PM

  18. Jarl Friis
  19. Santiago Pastorino
    Santiago Pastorino

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:49 PM

  20. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:49 PM

  21. Greg Hazel
    Greg Hazel

    The wish for this to be fixed still exists. Using default_url_options[:host] seems like a reasonable answer.

    [state:open]

    March 31st, 2011 @ 05:09 AM