Search code examples
ruby-on-railsrails-enginesrails-activestorageruby-on-rails-6

Undefined method 'attachment_path' for Rails engine model with ActiveStorage attachment


I have a Rails Engine and Rails app, both running Rails 6.0.0.rc1.

Engine defines model with ActiveStorage attachments, eg.

module Shop
  class Product < ApplicationRecord
    ...

    has_one_attached :image
    has_many_attached :images
  end
end

Using this model in both engine's and application views and trying to call = url_for(product.image) raises an exception undefined method 'attachment_path' for #<#<Class:XXX>:XXX>. product.image.attached? returns true, thus attachment is available.

This code was extracted from Rails application where it worked just fine. Is there a special route helper for Rails Engine model attachments or any setup other than rails active_storage:install needed not mentioned in the documentation?


Solution

  • I got this working using main_app.url_for(product.image)!

    Works both in engine views and main Rails app views.