Rails 带有 ActiveStorage 附件的引擎模型的未定义方法 'attachment_path'

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

我有一个 Rails 引擎和 Rails 应用程序,都是 运行 Rails 6.0.0.rc1.

引擎定义带有 ActiveStorage 附件的模型,例如。

module Shop
  class Product < ApplicationRecord
    ...

    has_one_attached :image
    has_many_attached :images
  end
end

在引擎和应用程序视图中使用此模型并尝试调用 = url_for(product.image) 会引发异常 undefined method 'attachment_path' for #<#<Class:XXX>:XXX>product.image.attached? returns 正确,因此附件可用。

此代码是从 Rails 应用程序中提取的,它工作得很好。 Rails 引擎模型附件或文档中未提及的 rails active_storage:install 以外的任何设置是否有特殊的路线助手?

我使用 main_app.url_for(product.image) 完成了这项工作!

在引擎视图和主要 Rails 应用程序视图中均有效。

作为其他人的参考,您可以阅读更多相关信息in the Rails Guides