简体   繁体   中英

Rails routes for non-standard assets in a mountable engine

I have a few non-standard assets (ie files that are not images/javascript files/stylesheets such as json and binary files) that live within a mountable engine (without isolate_namespace ) in app/assets/data . I want these to be part of the asset pipeline (in the same way as eg images). I can add them to the asset paths collection, eg

class Engine < ::Rails::Engine
  config.after_initialize do
    Rails.application.config.assets.paths << root.join("app", "assets", "data")
  end
end

and I can see in the Rails console that the assets are visible to the asset pipeline (eg via Rails.application.assets[] and ActionController::Base.helpers.asset_path ). For instance, for a file app/assets/data/foo.json , the asset_path helper in the rails console for the hosting app gives me a path assets/foo.json , however that path does not work, I get a

ActionController::RoutingError (No route matches [GET] "/assets/foo.json")

error.

How can I get the hosting Rails app to serve these files?

Turns out, this is some odd behavior with json files with specific names. The files in question are named something like schema-[UUID].json . Rails seems to think these are calls to some controller (even though there is no such route, nor a schema controller) that want json-formatted data back. When I rename the files to [UUID]-schema.json , they all of a sudden work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM