rails: Can't resolve image into URL when using ActiveStorage in mountable Engine

Steps to reproduce

  • Create a new 5.2 app and setup ActiveStorage using a simple form as described in this blogpost
  • Create a new mountable engine and move the PostsController and views into the engine’s namespace.
  • Add the newly created engine to your app’s Gemfile and mount the engine

Expected behavior

url_for should still work with ActiveStorage models.

Actual behavior

ArgumentError when using url_for or image_tag after form submission. Can't resolve image into URL: undefined method to_model’ for #<ActiveStorage::Variant`

Edit: It seems that if I don’t use isolate_namespace it works as expected. Are we not supposed to use ActiveStorage in isolated namespaces?

If I manually create the correct route based on ActiveStorage’s routes I can create the correct path through main_app:

variant = image.file.variant(resize: '100x100')
signed_blob_id = variant.blob.signed_id
variation_key  = variant.variation.key
filename       = variant.blob.filename

image_tag main_app.rails_blob_variation_path(signed_blob_id, variation_key, filename)

System configuration

Rails version: 5.2.0.beta2

Ruby version: 2.4.2

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 4
  • Comments: 15 (2 by maintainers)

Commits related to this issue

Most upvoted comments

Have you tried main_app.url_for(variant)?

Spina::ImagesHelper works fine on Rails 5.2 RC1. but return undefined method 'rails_blob_variation_path' on 5.2 RC2.

I try to this code, it works on my engine.

<%= image_tag image.variant(resize: '100x100').processed.service_url %>

so I’ve found someone advising that the image_tag has changed in v5.2+ Unlike v5.1 where you could pass an uploader-object and rails would guess, now in v5.2+ you can only pass the URL.

We’ve included a helper in Spina CMS to circumvent this problem for now. Is this something that will be picked up after the 5.2 release?

module Spina
  module ImagesHelper

    def variant(file, options)
      variant = file.variant(options)
      main_app.rails_blob_representation_path(variant.blob.signed_id, variant.variation.key, variant.blob.filename)
    end

  end
end

Apparently the name of the route changed to rails_blob_representation_path. Changing it in the variant method fixes it.

I added a method to our Spina::Image model so you can pass a PagePart directly to image_tag like this:

= image_tag content(:header_image).variant(resize: '100x100')

@pixeltrix https://github.com/Bramjetten/active_storage_test

It’s a simple new Rails 5.2 app with a Post model with has_one_attached. I placed the controller, views and routes inside a new engine (in the engines folder).

Bumping into this now with Rails 6.1.4 and a mountable engine with ActionText.