Define Default URL for ActiveStorage to fix Mixed Content Error

Using ActiveStorage with external services like AWS S3 for saving and surfing attachments like images is great. The setup is well explained in Active Storage Overview in Ruby on Rails Guides.

But if you start using it, you will maybe encounter some problems, which are not that easy to find or debug within production. One of the problems we encountered was a Mixed content related to ActiveStorage. This was not a big problem at first. But after we added an iOS App, which was using WkWebViews, some random redirects to AWS S3 images stopped working. We couldn’t even see connections for these images to ActiveStorage in logs, which made it harder to debug.

Find the problem

After some analysis we found some image redirect links to load with “HTTP” and most with “HTTPS”. The next step was to figure out, why it is happening and how to fix it.

Understand it

The first simple step for a developer is “ask a search engine” (Google/Bing/….). But the results were fuzzy and most of them were covering unrelated topics or were just “it works now” which I consider being a problem for most developers just trying the fix without understanding the issue, finding the right solution and write a test for it.

Documentation

I’ve checked the documentation for ActiveStorage and the Ruby on Rails Guides, but couldn’t find anything useful.

Project Issues

For me, it looked like an issue, that someone else might have had before. Let’s check GitHub issues for Ruby on Rails first. There I found this issue, which told me, that someone else had a similar problem and he tried to fix it by a pull request: Ruby on Rails Issue 42947. This pull request was closed. But I got a slight idea of where to look for the implementation, to figure out, how to fix this issue.

Check source code for implementation

The next step was to check the code, which might tell me, where I have to set the default URL options for the project. It was in the class DiskService on Line 168:

def url_helpers
  @url_helpers ||= Rails.application.routes.url_helpers
end

Solution

As the project was more of a monolith, for me it was enough to set the default URL options for routes and therefore define in for ActiveStorage too. As it was only relevant for production, I’ve added it on the top of config/environments/production.rb:


Rails.application.routes.default_url_options = { host: "www.my.project",
                                                 protocol: "https" }

After deploying the application to production the problem was gone and ActiveStorage used always the right URL https://www.my.project/.

Summary

Sometimes it’s not that easy to find a problem, which is randomized and maybe not a problem on all devices, operating systems browsers. You need a deep understanding to find it.

As most problems were encountered by other developers, you can try to search for it, check the documentation, check project issues, or to be sure check the source code itself, which is the most secure documentation you can have.